Serilog With API App in Azure Serilog With API App in Azure azure azure

Serilog With API App in Azure


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:

enter image description here


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.

enter image description here

Then open the Log stream and locate the Application logs.

You could find the log has already log into the log-steam.

enter image description here

The folder:

enter image description here