Skip to content

OrchardCore.Logging.Serilog

OrchardCore.Logging.Serilog

OrchardCore.Logging.Serilog integrates Serilog structured logging with OrchardCore

How to use

如何使用

add a reference to OrchardCore.Logging.Serilog

添加对'OrchardCore.Logging.Serilog`的引用

add serilog configuration in appsettings.json

在appsettings.json中添加serilog配置


  "Serilog": {

   <font color=#0099ff size=4 face="黑体">“Serilog”:{</font> 


    "MinimumLevel": {

      "Default": "Warning",

      "Override": {

        "Default": "Warning",

        "Microsoft": "Error",

        "System": "Error"

      }

    },

    "WriteTo": [

      {

        "Name": "Console",

        "Args": {

          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",

          "outputTemplate": "{Timestamp:HH:mm:ss}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",

          "restrictedToMinimumLevel": "Information"

        }

      },

      {

        "Name": "RollingFile",

        "Args": {

          "pathFormat": "app_data/logs/orchard-log-{Date}.txt",

          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",

          "restrictedToMinimumLevel": "Warning"

        }

      }

    ]

  }

   <font color=#0099ff size=4 face="黑体">}</font> 


Modify program.cs to use Serilog

修改program.cs以使用Serilog


        public static IWebHost BuildWebHost(string[] args)

            => WebHost.CreateDefaultBuilder(args)

                .UseSerilogWeb()

                .UseStartup<Startup>()

                .Build();

Modify startup.cs to include TenantName in LogContext

修改startup.cs以在LogContext中包含TenantName


        using OrchardCore.Logging;




 <font color=#0099ff size=4 face="黑体"></font> 


        public void Configure(IApplicationBuilder app, IHostingEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            app.UseStaticFiles();

            app.UseOrchardCore(c => c.UseSerilogTenantNameLoggingMiddleware());

        }