Skip to content

SpringHgui/serilog-sinks-skywalking

Repository files navigation

serilog-sinks-skywalking

Nuget

Write Serilog events to skywalking apm

使用

  1. 集成SkyAPM.Agent.AspNetCore
  2. 集成Serilog.AspNetCore
  3. 安装nuget包 Serilog.Sinks.Skywalking
public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseSerilog((context, services, configuration) =>
            {
                configuration
                .MinimumLevel.Debug()
                .Enrich.FromLogContext()
                .WriteTo.Skywalking(services) //添加这一行
                .WriteTo.Console();
            }) 
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

或者在 MiniApi项目中

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog((context, services, configuration) =>
{
    configuration
       .MinimumLevel.Debug()
       .Enrich.FromLogContext()
       .WriteTo.Skywalking(services) //添加这一行
       .WriteTo.Console();
});

高级

自定义Formater

configuration
    .WriteTo.Skywalking(services, new JsonFormatter())

更多Formatter实现查看Serilog文档,或自行实现ITextFormatter