.NET 6 API使用Serilog APM

本文介绍如何在.NET 6 API中使用Serilog的APM。

1. 引用Serilog相关的packages

csharp 复制代码
<PackageReference Include="Elastic.Apm.SerilogEnricher" Version="8.11.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.2" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="10.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="10.0.0" />

2. appsettings.json添加配置文件

csharp 复制代码
 "SeriLog": {
   "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Elasticsearch" ],
   "MinimumLevel": {
     "Default": "Information"
   },
   "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithElasticApmCorrelationInfo" ]
 },
 "SerilogSupplementer": {
   "WriteToFile": true,
   "LogFilePath": "c:/temp/logs/mes-api/mes-api-.json",
   "FileRestrictToMinimumLevel": "Information",
   "FileSizeLimitBytes": 10485760, //10 MB
   "RetainedFileTimeLimit": "10.00:00", // Retain files for a maximum of 10 days 0 Hrs 0 Mins
   "rollOnFileSizeLimit": true,
   "EnrichFromLogContext": true,
   "MinimumLevelControlledBy": null,
   "EnrichProperties": {
     "Application": "mes-api",
     "Environment": "Development"
   },
   "MinimumLevelOverrides": {
     "Microsoft.NetCore": "Warning",
     "Elastic.Apm": "Warning"
   },
   "EnrichWithExceptionDetails": true,
   "AddElasticApmCorrelationInfo": true,
   "WriteToConsole": true,
   "ConsoleFormat": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}"
 },
 "ElasticApm": {
   "ServerUrls": "http://localhost:8200", // URL of your APM server
   "ServiceName": "mes-api",
   "Environment": "Development",
   "CentralConfig": false,
   "LogLevel": "Error"
 },

3. 注册

3.1 创建SerilogSupplementerConfiguration.cs

csharp 复制代码
using Serilog.Core;
using Serilog.Events;

namespace MES.API.Logging;

public class SerilogSupplementerConfiguration
{
    public bool WriteToFile { get; set; }
    public string LogFilePath { get; set; } = string.Empty;
    public LogEventLevel FileRestrictedToMinimumLevel { get; set; } = LogEventLevel.Debug;
    public bool EnrichFromLogContext { get; set; } = true;
    public LoggingLevelSwitch? MinimumLevelControlledBy { get; set; }
    public Dictionary<string,object> EnrichProperties { get; set; } = new();
    public Dictionary<string,LogEventLevel> MinimumLevelOverrides { get; set; } = new();
    public bool EnrichWithExceptionDetails { get; set; } = true;
    public bool AddElasticApmCorrelationInfo { get; set; } 
    public bool WriteToConsole { get; set; }
    public string ConsoleFormat { get; set; } = string.Empty;
    public string RetainedFileTimeLimit { get; set; } = "10.00:00"; //Default to 10 days 
    public long FileSizeLimitBytes { get; set; } = 10485760; //Default to 10 MB
    public bool RollOnFileSizeLimit { get; set; }
}

3.2 注册

csharp 复制代码
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using AlterDomus.a360.Core.Logging;
using Serilog;
using Serilog.Exceptions;
using Elastic.Apm.SerilogEnricher;
using Serilog.Formatting.Elasticsearch;
using Serilog.Exceptions.Core;

namespace MES.API.Extensions;
public static class MESExtensions
{
    /// <summary>
    /// Add Serilog ILogger using configuration. SerilogSupplementerConfiguration will be read from configuration if exists.
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    /// <param name="logArgs"></param>
    public static IServiceCollection AddSerilog(this IServiceCollection services, IConfiguration configuration)
    {
        var serilogConfig = new Serilog.LoggerConfiguration()
            .ReadFrom.Configuration(configuration);

        var serilogSupplementer = configuration.GetSection("SerilogSupplementer").Get<SerilogSupplementerConfiguration>() ?? new();
        var retainedFileTimeLimit = TimeSpan.Parse(serilogSupplementer.RetainedFileTimeLimit);
        var fileSizeLimitBytes = serilogSupplementer.FileSizeLimitBytes;
        var rollOnFileSizeLimit = serilogSupplementer.RollOnFileSizeLimit;

        if (serilogSupplementer != null)
        {
            if (serilogSupplementer.MinimumLevelControlledBy != null)
            {
                serilogConfig.MinimumLevel.ControlledBy(serilogSupplementer.MinimumLevelControlledBy);
            }
            foreach (var kvp in serilogSupplementer.MinimumLevelOverrides)
            {
                serilogConfig.MinimumLevel.Override(kvp.Key, kvp.Value);
            }
            if (serilogSupplementer.EnrichFromLogContext)
            {
                serilogConfig.Enrich.FromLogContext();
            }
            if (serilogSupplementer.AddElasticApmCorrelationInfo)
            {
                serilogConfig.Enrich.WithElasticApmCorrelationInfo();
            }
            if (serilogSupplementer.WriteToConsole || !string.IsNullOrEmpty(serilogSupplementer.ConsoleFormat))
            {
                if (string.IsNullOrEmpty(serilogSupplementer.ConsoleFormat))
                {
                    serilogConfig.WriteTo.Console();
                }
                else
                {
                    serilogConfig.WriteTo.Console(outputTemplate: serilogSupplementer.ConsoleFormat);
                }
            }
            if (serilogSupplementer.EnrichWithExceptionDetails)
            {
                serilogConfig.Enrich.WithExceptionDetails(
                    new DestructuringOptionsBuilder().WithDefaultDestructurers()
                );
            }
            if (serilogSupplementer.WriteToFile)
            {
                serilogConfig.WriteTo.File(
                   new ElasticsearchJsonFormatter(renderMessage: true, renderMessageTemplate: false),
                   path: serilogSupplementer.LogFilePath,
                   restrictedToMinimumLevel: serilogSupplementer.FileRestrictedToMinimumLevel,
                   rollingInterval: RollingInterval.Day,
                   fileSizeLimitBytes: fileSizeLimitBytes,
                   retainedFileTimeLimit: retainedFileTimeLimit,
                   rollOnFileSizeLimit: rollOnFileSizeLimit,
                   shared: true
               );
            }
            foreach (var kvp in serilogSupplementer.EnrichProperties)
            {
                serilogConfig.Enrich.WithProperty(kvp.Key, kvp.Value);
            }
            if (!serilogSupplementer.EnrichProperties.ContainsKey("UserName"))
            {
                serilogConfig.Enrich.WithProperty("UserName", Environment.UserName);
            }
        }

        ILogger logger = serilogConfig.CreateLogger();

        services.AddSingleton<Serilog.ILogger>(logger);
        return services;
    }

}

3.3 在program.cs使用

csharp 复制代码
     builder.Services.AddSerilog(builder.Configuration);
     builder.Host.UseSerilog();
     builder.Services.AddAllElasticApm();
相关推荐
geats人山人海5 小时前
c# 第九章 record
开发语言·c#
-银雾鸢尾-6 小时前
C#中的抽象类与抽象方法
开发语言·c#
影寂ldy7 小时前
C# Task 进阶:WaitAll / WaitAny / WhenAll / WhenAny
开发语言·c#
fen_fen7 小时前
麒麟 Linux aarch64安装.NET 6,并适配.net应用(达梦数据库)
linux·服务器·.net
o5278831848 小时前
校园一体化管理系统2.1、基于整洁四层架构 + DDD+CQRS 项目目录解读
后端·架构·c#·asp.net·visual studio
农村小镇哥1 天前
C#中的字符串格式化
服务器·开发语言·c#
czhc11400756631 天前
719:StartPoint;Margin;ResourceDictionar;Component.model;lds;
c#
-银雾鸢尾-1 天前
C#中的万物之父——Object类
开发语言·c#
水晶石NSIS专栏1 天前
Geek_Studio - 安卓终端调试工作台
android·c#·apk签名·coloros主题