namespaceSerilog.Events { ///<summary> /// Specifies the meaning and relative importance of a log event. ///</summary> publicenum LogEventLevel { ///<summary> /// Anything and everything you might want to know about /// a running block of code. ///</summary> Verbose,
///<summary> /// Internal system events that aren't necessarily /// observable from the outside. ///</summary> Debug,
///<summary> /// The lifeblood of operational intelligence - things /// happen. ///</summary> Information,
///<summary> /// Service is degraded or endangered. ///</summary> Warning,
///<summary> /// Functionality is unavailable, invariants are broken /// or data is lost. ///</summary> Error,
///<summary> /// If you have a pager, it goes off when one of these /// occurs. ///</summary> Fatal } }
// Set all the common properties available for every request diagnosticContext.Set("Host", request.Host); diagnosticContext.Set("Protocol", request.Protocol); diagnosticContext.Set("Scheme", request.Scheme);
// Only set it if available. You're not sending sensitive data in a querystring right?! if(request.QueryString.HasValue) { diagnosticContext.Set("QueryString", request.QueryString.Value); }
// Set the content-type of the Response at this point diagnosticContext.Set("ContentType", httpContext.Response.ContentType);
// Retrieve the IEndpointFeature selected for the request var endpoint = httpContext.GetEndpoint(); if (endpoint isobject) // endpoint != null { diagnosticContext.Set("EndpointName", endpoint.DisplayName); } }; });
Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) // Filter out ASP.NET Core infrastructre logs that are Information and below .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.Seq("http://localhost:5341") .CreateLogger();
publicstaticclassLogHelper { publicstatic LogEventLevel ExcludeHealthChecks(HttpContext ctx, double _, Exception ex) => ex != null ? LogEventLevel.Error : ctx.Response.StatusCode > 499 ? LogEventLevel.Error : IsHealthCheckEndpoint(ctx) // Not an error, check if it was a health check ? LogEventLevel.Verbose // Was a health check, use Verbose : LogEventLevel.Information;
privatestaticboolIsHealthCheckEndpoint(HttpContext ctx) { var endpoint = ctx.GetEndpoint(); if (endpoint isobject) // same as !(endpoint is null) { returnstring.Equals( endpoint.DisplayName, "Health checks", StringComparison.Ordinal); } // No endpoint, so not a health check endpoint returnfalse; } }
// program.cs using seq_demo.Filters; using seq_demo.Helper; using Serilog; using Serilog.Events;
// 指定 LOG 輸出到 Console 及 Seq Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) // Filter out ASP.NET Core infrastructre logs that are Information and below .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.Seq("http://localhost:5341") .CreateLogger();
try { var builder = WebApplication.CreateBuilder(args); // 使用 Serilog builder.Host.UseSerilog();
// Add services to the container. builder.Services.AddControllersWithViews(config => { // 註冊 serilog action filter config.Filters.Add(typeof(SerilogLoggingActionFilter)); });
var app = builder.Build();
// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); }
// Set all the common properties available for every request diagnosticContext.Set("Host", request.Host); diagnosticContext.Set("Protocol", request.Protocol); diagnosticContext.Set("Scheme", request.Scheme);
// Only set it if available. You're not sending sensitive data in a querystring right?! if(request.QueryString.HasValue) { diagnosticContext.Set("QueryString", request.QueryString.Value); }
// Set the content-type of the Response at this point diagnosticContext.Set("ContentType", httpContext.Response.ContentType);
// Retrieve the IEndpointFeature selected for the request var endpoint = httpContext.GetEndpoint(); if (endpoint isobject) // endpoint != null { diagnosticContext.Set("EndpointName", endpoint.DisplayName); } };