Serilog - Output/Enrich All Messages with MethodName from which log entry was Called

后端 未结 5 1753
面向向阳花
面向向阳花 2020-12-02 16:24

Is there anyway to enrich all Serilog output with the Method Name.

For Instance consider If I have the following;

Public Class MyClassName

  Privat         


        
5条回答
  •  情歌与酒
    2020-12-02 16:35

    The version on C# could be simplified. Just use AutofacSerilogIntegration:

    var path = Server.MapPath("~/");
    
            var outputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} {Message} {NewLine}{Exception}";
            Log.Logger = new LoggerConfiguration()
                        .MinimumLevel.Debug()
                        .WriteTo.File($"{path}/log/serilog-.log", LogEventLevel.Debug, outputTemplate, rollingInterval: RollingInterval.Day)
                        .CreateLogger();
    
            var builder = new ContainerBuilder();
            builder.RegisterLogger();  // from AutofacSerilogIntegration
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    

提交回复
热议问题