Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog With API App in Azure

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?

like image 759
ZENIT Avatar asked Oct 08 '17 09:10

ZENIT


People also ask

How is Serilog implemented in .NET core?

Configure Serilog in ASP.NET Core By specifying the configuration in the appsettings. json file we will be able to change the configuration or add/remove sinks without code changes. Now change the code in the program. cs to specify Host Builder to use Serilog in ASP.NET Core instead of default logger.

Does Serilog use ILogger?

But to my surprise, Serilog is using it's own ILogger interface - bummer!

How do you use Serilog logging?

Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.

Can Azure Functions send logs to serilog?

Azure Functions are great, except logging configuration is not the same as .Net Core applications and there are a number of ways logging can be configured incorrectly which leads to unexpected behavior. One such scenario I recently experienced was sending the Function App logs to Serilog.

What is a serilog provider?

One of these is the Serilog provider. Serilog is a notable logging technology both because it is a structured logging solution and because of the wide variety of custom sinks it supports. Most Serilog sinks now support .NET Standard.

How do I enable application logging in azure?

Application logging can be enabled in the Azure portal under the app service’s ‘Diagnostic logs’ page. Logging can be sent either to the file system or blob storage. Blob storage is a better option for longer-term diagnostic storage, but logging to the file system allows logs to be streamed.

What are the Azure App Service logging providers available for core?

The Azure app service logging provider is one example of a useful logging extension available for ASP.NET Core. Of course, if your app is not run as an Azure app service (perhaps it’s run as a microservice in Azure Container Service, for example), you will need other logging providers. Fortunately, ASP.NET Core has many to choose from.


1 Answers

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

like image 189
Brando Zhang Avatar answered Sep 22 '22 21:09

Brando Zhang