Serilog With API App in Azure
Asked Answered
R

1

16

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?

Ramburt answered 8/10, 2017 at 9:20 Comment(1)
Did you try with .WriteTo.Trace()? This should write to Log stream as it shows whats been written to Trace.Jackijackie
E
19

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

Encephalo answered 9/10, 2017 at 8:55 Comment(6)
Your solution will work, of course. However it still remains unclear (for me) how to utilize Log Streaming feature in Azure with SerilogRamburt
As far as I know, if you want to use log Streaming feature in Azure, you could only enable the 'Diagnostic logs' in web app.Encephalo
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. More details, you could see my update answer.Encephalo
I have similar requirements. I would like to know how do we manage logs when the connection to the cloud fails due to network issues? will it be logged in local on-premise server and sent once connection establish back? second question is, does audit log also can be sent to cloud using serilog? is it mandatory to have database for audit logs?Darken
RollingFile is now deprecated please update yiur answer.Sisyphean
Where do you find your application folder? If I mean, not locally? I can't find them on Azure.Amazon

© 2022 - 2024 — McMap. All rights reserved.