在 Blazor ServerSide 中连接到 SignalR 事件

Hooking into SignalR event in Blazor ServerSide

我找到了微软提供的默认日志记录机制的这段代码:

public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
             .ConfigureLogging(logging =>
             {
                 logging.ClearProviders();
                 logging.AddConsole();
                 logging.AddFilter(
                   "Microsoft.AspNetCore.SignalR", LogLevel.Trace);
                 logging.AddFilter(
                   "Microsoft.AspNetCore.Http.Connections",
                   LogLevel.Trace);
             })
             .ConfigureWebHostDefaults(webBuilder =>
             {
                 webBuilder.UseStartup<Startup>();
             });

我的Serilog设置如下:

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            using IHost host = CreateHostBuilder(args).Build();
            host.Run();
        }
        catch (Exception ex)
        {                
            if (Log.Logger == null || Log.Logger.GetType().Name == "SilentLogger")
            {
                Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .WriteTo.Console()
                    .CreateLogger();
            }

            Log.Fatal(ex, "Host terminated unexpectedly");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    private static IHostBuilder CreateHostBuilder(string[] args)
       => Host.CreateDefaultBuilder(args)
              .UseServiceProviderFactory(new AutofacServiceProviderFactory(Register))
              .ConfigureWebHostDefaults(webBuilder =>
              {
                webBuilder.UseStartup<Startup>()
                   .CaptureStartupErrors(true)
                   .ConfigureAppConfiguration(config => { config.AddJsonFile("appsettings.Local.json", optional: true); })
                   .UseSerilog((hostingContext, loggerConfiguration) =>
                   {
                       loggerConfiguration
                           .ReadFrom.Configuration(hostingContext.Configuration)
                           .Enrich.FromLogContext()
                           .Enrich.WithProperty("ApplicationName", typeof(Program).Assembly.GetName().Name)
                           .Enrich.WithProperty("Environment", hostingContext.HostingEnvironment);

                        #if DEBUG                           
                       loggerConfiguration.Enrich.WithProperty("DebuggerAttached", Debugger.IsAttached);
                        #endif 
                   });
              });

    private static void Register(ContainerBuilder builder) => builder.RegisterLogger(autowireProperties: true);

}

如何将 SignalR 过滤器添加到我的设置中?

在 Serilog 术语中,您需要覆盖 Microsoft SignalR 命名空间的最低级别。

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.UseSerilog((context, config) =>
   {
     /*
     * the rest of the Serilog configuration here
     */
     config.MinimumLevel.Override("Microsoft.AspNetCore.SignalR", LogEventLevel.Debug);
     config.MinimumLevel.Override("Microsoft.AspNetCore.Http.Connections", LogEventLevel.Debug);
   })
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });