@@ -23,9 +23,14 @@ public static void RegisterSerilog(this WebApplicationBuilder builder)
2323 {
2424 Serilog . Debugging . SelfLog . Enable ( msg => Console . WriteLine ( msg ) ) ;
2525 builder . Host . UseSerilog ( ( context , configuration ) =>
26- configuration . ReadFrom . Configuration ( context . Configuration )
27- . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Error )
26+ configuration
27+ // Configure minimum log levels
28+ . MinimumLevel . Information ( )
29+ . MinimumLevel . Override ( "Microsoft" , LogEventLevel . Warning )
2830 . MinimumLevel . Override ( "Microsoft.AspNetCore" , LogEventLevel . Error )
31+ . MinimumLevel . Override ( "Microsoft.Hosting.Lifetime" , LogEventLevel . Warning )
32+ . MinimumLevel . Override ( "System" , LogEventLevel . Warning )
33+ . MinimumLevel . Override ( "System.Net.Http.HttpClient" , LogEventLevel . Warning )
2934 . MinimumLevel . Override ( "MudBlazor" , LogEventLevel . Information )
3035 . MinimumLevel . Override ( "Serilog" , LogEventLevel . Information )
3136 . MinimumLevel . Override ( "Microsoft.EntityFrameworkCore.AddOrUpdate" , LogEventLevel . Error )
@@ -42,9 +47,14 @@ public static void RegisterSerilog(this WebApplicationBuilder builder)
4247 . MinimumLevel . Override ( "ActualLab.Fusion.Internal.ComputedGraphPruner" , LogEventLevel . Error )
4348 . MinimumLevel . Override ( "CleanArchitecture.Blazor.Server.UI.Services.Fusion.UserSessionTracker" , LogEventLevel . Error )
4449 . MinimumLevel . Override ( "CleanArchitecture.Blazor.Server.UI.Services.Fusion.OnlineUserTracker" , LogEventLevel . Error )
50+ // Add enrichment properties
4551 . Enrich . FromLogContext ( )
4652 . Enrich . WithUtcTime ( )
4753 . Enrich . WithUserInfo ( )
54+ . Enrich . WithProperty ( "Application" , "BlazorApp" )
55+ . Enrich . WithProperty ( "Environment" , context . HostingEnvironment . EnvironmentName )
56+ . Enrich . WithProperty ( "TargetFramework" , "net9" )
57+ // Configure output sinks
4858 . WriteTo . Async ( wt => wt . File ( "./log/log-.txt" , rollingInterval : RollingInterval . Day ) )
4959 . WriteTo . Async ( wt =>
5060 wt . Console (
@@ -56,9 +66,30 @@ public static void RegisterSerilog(this WebApplicationBuilder builder)
5666
5767 private static void ApplyConfigPreferences ( this LoggerConfiguration serilogConfig , IConfiguration configuration )
5868 {
69+ WriteToSeq ( serilogConfig , configuration ) ;
5970 WriteToDatabase ( serilogConfig , configuration ) ;
6071 }
6172
73+ private static void WriteToSeq ( LoggerConfiguration serilogConfig , IConfiguration configuration )
74+ {
75+ var serverUrl = "https://seq.blazorserver.com" ;
76+ var apiKey = "none" ;
77+ var restrictedToMinimumLevel = "Verbose" ;
78+
79+ if ( ! string . IsNullOrEmpty ( serverUrl ) )
80+ {
81+ var minimumLevel = Enum . TryParse < LogEventLevel > ( restrictedToMinimumLevel , true , out var level )
82+ ? level
83+ : LogEventLevel . Verbose ;
84+
85+ serilogConfig . WriteTo . Seq (
86+ serverUrl ,
87+ apiKey : string . IsNullOrEmpty ( apiKey ) || apiKey == "none" ? null : apiKey ,
88+ restrictedToMinimumLevel : minimumLevel
89+ ) ;
90+ }
91+ }
92+
6293 private static void WriteToDatabase ( LoggerConfiguration serilogConfig , IConfiguration configuration )
6394 {
6495 if ( configuration . GetValue < bool > ( "UseInMemoryDatabase" ) ) return ;
@@ -144,8 +175,8 @@ private static void WriteToNpgsql(LoggerConfiguration serilogConfig, string? con
144175 if ( string . IsNullOrEmpty ( connectionString ) ) return ;
145176
146177 const string tableName = "loggers" ;
147- //Used columns (Key is a column name)
148- //Column type is writer's constructor parameter
178+ // Used columns (Key is a column name)
179+ // Column type is writer's constructor parameter
149180 IDictionary < string , ColumnWriterBase > columnOptions = new Dictionary < string , ColumnWriterBase >
150181 {
151182 { "message" , new RenderedMessageColumnWriter ( NpgsqlDbType . Text ) } ,
@@ -210,8 +241,8 @@ internal class UserInfoEnricher : ILogEventEnricher
210241 public UserInfoEnricher ( ) : this ( new HttpContextAccessor ( ) )
211242 {
212243 }
213- //Dependency injection can be used to retrieve any service required to get a user or any data.
214- //Here, I easily get data from HTTPContext
244+ // Dependency injection can be used to retrieve any service required to get a user or any data.
245+ // Here, I easily get data from HTTPContext
215246 public UserInfoEnricher ( IHttpContextAccessor httpContextAccessor )
216247 {
217248 _httpContextAccessor = httpContextAccessor ;
0 commit comments