serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
457 stars 129 forks source link

dynamic file name #35

Closed cwj92 closed 7 years ago

cwj92 commented 7 years ago

hi,

in startup configuration we can set the file name to include today's date like so: .WriteTo.RollingFile("logs\\api-{Date}.txt")

but in appsettings.json seems not working, the file name is just static

api-{Date}.txt

"WriteTo": [
      { "Name": "LiterateConsole" },
      {
        "Name": "File",
        "Args": { "path": "logs\\api-{Date}.txt" }
      }
    ],

anyway to achieve this in appsettings.json?

thanks.

skomis-mm commented 7 years ago

@cwj92 , RollingFile supports this:

{
    "Name": "RollingFile",
    "Args": { "pathFormat": "logs\\api-{Date}.txt" }
}
cwj92 commented 7 years ago

@skomis-mm thanks a lot

seangwright commented 7 years ago

I haven't been able to get this to work

Here is my appsettings.json

"Serilog": {
    "Using": [ "Serilog.Sinks.Literate" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "LiterateConsole" },
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "serilogs\\Proj.Api-{Date}.txt"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
      "Application": "Proj"
    }
  }
}

Here are my packages in .csproj

    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
    <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.0.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="1.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="2.2.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="3.1.0" />
    <PackageReference Include="Serilog.Sinks.Literate" Version="2.1.0" />
    <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />

And here is my configuration in my Startup

Log.Logger = new LoggerConfiguration()
     .ReadFrom.Configuration(Configuration)
     .CreateLogger();

Nothing appears in my project\serilogs\ directory but plenty appears in my console when running my API.

Is it my pathFormat? I'm running this on Windows (not sure if that matters).

nblumhardt commented 7 years ago

@sgwatgit is it possible the process doesn't have permission to create or write to the target path? (Always one thing worth checking..).

You might find some information in SelfLog.

Since the original issue is closed, if this doesn't help would you mind please opening another, or posting more details to Stack Overflow?

All the best, Nick

seangwright commented 7 years ago

@nblumhardt Thanks - The SelfLog reported an error

2017-03-24T06:55:49.4090207Z Caught exception while emitting to sink Serilog.Sinks.RollingFile.RollingFileSink: System.TypeLoadException: Could not load type 'Serilog.Sinks.File.SharedFileSink' from assembly 'Serilog.Sinks.File, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10'.
   at Serilog.Sinks.RollingFile.RollingFileSink.OpenFile(DateTime now)
   at Serilog.Sinks.RollingFile.RollingFileSink.AlignCurrentFileTo(DateTime now)
   at Serilog.Sinks.RollingFile.RollingFileSink.Emit(LogEvent logEvent)
   at Serilog.Core.Sinks.SafeAggregateSink.Emit(LogEvent logEvent)

I guess I had an older version of the package installed?

I cleaned my solution, restored nuget packages again and re-built/ran the project. That solved it - the files are being written now. Maybe my config above will be helpful to someone else looking to go the settings config route.

Thanks for the tip on SelfLog. Serilog's been great working with this evening so I imagine I'll continue using it and I think the SelfLog diagnostics will be very helpful.

Thanks for all your work on this.