Serilog RollingFile

前端 未结 7 1296
囚心锁ツ
囚心锁ツ 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条回答
  • 2021-01-07 19:48

    Try below:

     var log = new LoggerConfiguration()
              .MinimumLevel.Debug()
              .WriteTo.File(@"f:\log\log.txt", rollingInterval: RollingInterval.Day) 
              .CreateLogger();
    

    The log file name will be automatically log-20150819.txt etc. You do not need to specify the date.Old files will be cleaned up as per retainedFileCountLimit - default is 31.

    0 讨论(0)
  • 2021-01-07 19:51

    To enable multi-process shared log files, set shared to true:

    in code

    .WriteTo.RollingFile("log-{Date}.txt", shared: true)
    

    or in web.config

    <add key="serilog:write-to:RollingFile.shared" value="true" />
    
    0 讨论(0)
  • 2021-01-07 19:55

    Here is a way to use Serilog with a web.config in an asp.net MVC 4/5 app.

    In your web.config add the following:

    <add key="serilog:minimum-level" value="Information" />
    <add key="serilog:minimum-level:override:Microsoft" value="Information" />
    <add key="serilog:minimum-level:override:System" value="Information" />
    <add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
    <add key="serilog:write-to:RollingFile.pathFormat" value="./Logs/log-{Date}.txt" />
    <add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" />
    <add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />
    

    Then in Application_Start of global.asax add the following:

    // Get application base directory
    string basedir = AppDomain.CurrentDomain.BaseDirectory;
    
    // Setup Serilog for logging
    Log.Logger = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .WriteTo.RollingFile(basedir + "/Logs/log-{Date}.txt")
                .CreateLogger();
    
    0 讨论(0)
  • 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" );
          } )
      );   
    
    0 讨论(0)
  • 2021-01-07 20:04

    To use the same file, you have to add shared: true

    .WriteTo.RollingFile("log-{Date}.txt", shared: true)

    0 讨论(0)
  • 2021-01-07 20:09

    Now in 2018, the standard Serilog.Sinks.File NuGet package supports rolling:

    .WriteTo.File(@"e:\logs\skilliam.log", rollingInterval: RollingInterval.Day,
        rollOnFileSizeLimit: true, fileSizeLimitBytes: 123456);
    
    0 讨论(0)
提交回复
热议问题