I've integrated Serilog into WebApi project developed with Asp.Net Core 2.0
This is the configuration code in Program.cs
:
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
I manage to see the logs during the debug perfectly.
Now I deploy the service in Azure as API app.
What configuration should be applied to see the logs in the production environment through Log Stream extension in Azure Portal?
As far as I know, Serilog.Sinks.Console will write log events to the Windows Console. But if you publish the application to azure, we will not see the console directly.
I suggest you could consider using Serilog.Sinks.RollingFile or Serilog.Sinks.ApplicationInsights instead of the console to write log events .
About how to use Serilog.Sinks.RollingFile or Serilog.Sinks.ApplicationInsights, you could refer to below codes.
Firstly, install the Serilog.AspNetCore and Serilog.Sinks.RollingFile package from Nuget.
Then you could use below codes to log the information.
//if you want to use ApplicationInsights just change the write to's method as Serilog.Sinks.ApplicationInsights links shows
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.RollingFile("log-{Date}.txt")
.CreateLogger();
Log.Information("This will be written to the rolling file set");
It will auto create txt file to log the events.
Result like this, you could find it in the application wwwrot path:
Update:
If you want to use Serilog to log the logs to the azure log stream, you need firstly enable the 'Diagnostic logs' in web app. Then you could use Serilog to log the file to the azure default 'Diagnostic logs' folder. e.g: D:\home\LogFiles\http\RawLogs. Then the log will show in the Log Streaming.
Use below codes to test:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.File(@"D:\home\LogFiles\http\RawLogs\log.txt")
.CreateLogger();
Log.Information("This will be written to the rolling file set");
And enable the Diagnostics logs.
Then open the Log stream and locate the Application logs.
You could find the log has already log into the log-steam.
The folder: