Serilog RollingFile

前端 未结 7 1295
囚心锁ツ
囚心锁ツ 2021-01-07 19:45

I am trying to use WriteTo.RollingFile with Serilog as the following:

var log = new LoggerConfiguration().WriteTo.RollingFile(
                    @\"F:\\log         


        
7条回答
  •  -上瘾入骨i
    2021-01-07 19:57

    This is how I did it:

    private readonly Serilog.ILogger _logger; //= Log.ForContext( "Name", "Weather" );
    
    public WeatherForecastController() {
      string subPath = Path.Combine( DateTime.Now.ToString( "yyyy" ), DateTime.Now.ToString( "MM" ) ) + $"/{DateTime.Now.ToString("dd")}_Weather";
      _logger = Log.ForContext( "Name", subPath );
    }
    

      .UseSerilog( ( hostingContext, loggerConfiguration ) => loggerConfiguration
        .ReadFrom.Configuration( hostingContext.Configuration )
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .WriteTo.Map(
          "Name",
          "Request",
          ( name, wt ) => {
            if (name == "Request")
              wt.RollingFile( Path.Combine( $"{hostingContext.Configuration["LogPath"]}/{{Date}}-{name}.txt" ) );
            else
              wt.File( $"{hostingContext.Configuration["LogPath"]}/{name}.txt" );
          } )
      );   
    

提交回复
热议问题