Here is a way to use Serilog with web.config in asp.net MVC 4/5.
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 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();
source share