Achraf Ben Alaya
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About
    • Resume
SUBSCRIBE
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About
    • Resume
No Result
View All Result
Achraf Ben Alaya
No Result
View All Result
ADVERTISEMENT
Home Blog

Setting up Serilog in ASP.NET Core

achraf by achraf
September 15, 2020
in Blog, c#
4 min read
0
Setting up Serilog in ASP.NET Core
0
SHARES
2.1k
VIEWS
Share on FacebookShare on Twitter

WHAT TO LOG

It is not enough to log errors in order to use them for troubleshooting purposes. It is also useful to log successful requests so we can have a clear idea of how users work with the application .

Logs are also useful to detect  mistakes that users make, as well as for security purposes. Writing good logs about a user’s activity can alert us about malicious activity or any other kind of problems that we didn’t detect .

Why use Serilog ?

Serilog provides basic diagnostic logging not only to the console, files, Amazon, Azure, but more ,it’s easy to integrate and to use in a few steps .

This article will demonstrate the console and file sinks. It is easy as 1, 2, 3 to set up with appsettings configurations.

Steps : 

 

First thing that we are going to do is to create a new empty solution (Web Api )

serielog
serielog
serielog
serielog

Now we need to use the package manager to install some extensions as in pictures below or you can use the command :

Serilog.AspNetCore package

 

serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog
serielog

Logger initialization in Program.cs :

To start working with Serielog we need to initilize it first in the program.cs  .
You can copy replace your code with the below code :

public static void Main(string[] args)
       {
           var configuration = new ConfigurationBuilder()
           .AddJsonFile("appsettings.json")
           .Build();

           Log.Logger = new LoggerConfiguration()
               .ReadFrom.Configuration(configuration).WriteTo.Console().CreateLogger();

           try
           {
               Log.Information("Application startinng up");
               CreateHostBuilder(args).Build().Run();
           }
           catch (Exception ex)
           {

               Log.Fatal(ex, "The application failed to start correctly");
           }
           finally
           {
               Log.CloseAndFlush();

           }



       }

Also , we need to add : .UseSerilog() inside the CreateHostBuilder :

public static IHostBuilder CreateHostBuilder(string[] args) =>
           Host.CreateDefaultBuilder(args)
           .UseSerilog()
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   webBuilder.UseStartup<Startup>();
               });

 

Next , inside the Startup.cs we need to add : app.UseSerilogRequestLogging();

serielog
serielog

 

Now if we run our solution , we should see inside the console a message saying : Application startinng up ,

if you remember we added a line of code that will fire when the application start :

Log.Information("Application startinng up");

 

serielog
serielog

 

Cleaning up The default logger :

There are a few more changes that we are going to do and now inside the appsettings.json .

Serilog completely replaces the logging implementation on .NET Core,it will not work together  but it will replace the default implementation . The benefit of this is that you’re not running two different logging frameworks with tricky edge cases where they overlap in functionality.

{
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/log.txt",
          "rollingInterval": "Day",
          "outputTemplate": "{Timestamp:G} {Message}{NewLine:1}{Exception:1}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/log.json",
          "rollingInterval": "Day",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      }
   
    ]
  }
}

The “Name” key here specifies the sink desired. To write to multiple sinks, add another object to the “WriteTo” array , in our case we want to create files inside a logs folder and each day we want to create a new different file that contain the date time .

The MinimumLevel configuration object provides for one of the log event levels to be specified as the minimum.

If you see ,we have “path“: “Logs/log.txt”, where we define where we want our file to be written to and stored .

rollingInterval here mean that we want to create a new file per day .

as you can see inside write to , we defined to object , one that will write to a text file and another one that’s going to write inside a json file .

Now let’s run our solution :

 

serielog
serielog
serielog
serielog

As you can see there is a new Folder created : Logs , that contain a json file and a txt file .
let explore those 2 files :

{"Timestamp":"2020-09-13T15:04:23.3942682+01:00","Level":"Information","MessageTemplate":"Application startinng up"}
{"Timestamp":"2020-09-13T15:04:25.9319025+01:00","Level":"Information","MessageTemplate":"Getting WeatherForecast details","Properties":{"ActionId":"9612d1c8-4c4e-44b7-b7fd-374fe905687c","ActionName":"SerielogDemoDay.Controllers.WeatherForecastController.Get (SerielogDemoDay)","RequestId":"0HM2NTNM09MCA:00000001","RequestPath":"/weatherforecast","SpanId":"|b35b87a8-42a0adeaed09085c.","TraceId":"b35b87a8-42a0adeaed09085c","ParentId":""}}
{"Timestamp":"2020-09-13T15:04:25.9642904+01:00","Level":"Information","MessageTemplate":"HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms","Properties":{"RequestMethod":"GET","RequestPath":"/weatherforecast","StatusCode":200,"Elapsed":134.7792,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HM2NTNM09MCA:00000001","SpanId":"|b35b87a8-42a0adeaed09085c.","TraceId":"b35b87a8-42a0adeaed09085c","ParentId":""},"Renderings":{"Elapsed":[{"Format":"0.0000","Rendering":"134.7792"}]}}

As You can see the json file is very detailed file that contain all the event that happened .

while the txt file below , contain the actions with date time .

09/13/2020 15:04:23 Application startinng up
09/13/2020 15:04:25 Getting WeatherForecast details
09/13/2020 15:04:25 HTTP "GET" "/weatherforecast" responded 200 in 134.7792 ms

and now you can log anywhere inside your application by just using :

Log.Debug("debug test!");
Log.Information("Informations here.....!");
Log.Error("Error here ....!");

 

Link to demo project .

ShareTweet
Previous Post

What’s new in Microsoft Teams

Next Post

Learn… and get rewarded

Related Posts

Block the default URL assigned to the azure web app using azure application gateway
Azure

Block the default URL assigned to the azure web app using azure application gateway

May 31, 2023
56
Block the default URL assigned to the azure web app
Blog

Block the default URL assigned to the azure web app

May 14, 2023
215
What if we can check the cost of Azure resources in a juts few seconds
Azure

What if we can check the cost of Azure resources in a juts few seconds

May 9, 2023
331
Part 5-A : Using Azure DevOps, Automate Your CI/CD Pipeline and Your Deployments
Azure

Part 5-C : Using Azure DevOps, Automate Your CI/CD Pipeline and Your Deployments

May 2, 2023
216
15 E-books that focus on learning Microsoft Azure Cloud
Azure

15 E-books that focus on learning Microsoft Azure Cloud

April 23, 2023
102
Part 5-A : Using Azure DevOps, Automate Your CI/CD Pipeline and Your Deployments
Cloud

Part 5-B : Using Azure DevOps, Automate Your CI/CD Pipeline and Your Deployments

April 20, 2023
123
Next Post
Learn… and get rewarded

Learn... and get rewarded

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Terraform

Certifications

Microsoft certified trainer (MCT)

Recommended

Deploy azure function from visual studio 2019

Deploy azure function from visual studio 2019

August 29, 2020
455
Migrate and modernize your applications on Azure

Migrate and modernize your applications on Azure

March 26, 2021
372
Win a free certifications at the Microsoft spring skills challenge  🎁

Win a free certifications at the Microsoft spring skills challenge 🎁

March 20, 2022
417
Background Tasks With Hangfire And .Net 5

Background Tasks With Hangfire And .Net 5

January 25, 2021
2.5k
Installing WordPress with docker image of XAMPP

Installing WordPress with docker image of XAMPP

April 21, 2020
780
Sql tips and tricks

Sql tips and tricks

April 26, 2020
174
Facebook Twitter LinkedIn Youtube
Block the default URL assigned to the azure web app using azure application gateway

Block the default URL assigned to the azure web app using azure application gateway

May 31, 2023
Block the default URL assigned to the azure web app

Block the default URL assigned to the azure web app

May 14, 2023
What if we can check the cost of Azure resources in a juts few seconds

What if we can check the cost of Azure resources in a juts few seconds

May 9, 2023

Categories

  • Apps (1)
  • Azure (46)
  • blazor (2)
  • Blog (70)
  • c# (7)
  • Cloud (45)
  • Dapr (4)
  • docker (3)
  • Games (1)
  • General Tips & Fix (1)
  • Kubernetes Service (AKS) (1)
  • motivation (2)
  • Motivation (3)
  • News (9)
  • Resume (1)
  • sql (4)
  • Terrafrom (1)
  • Tricks, Tips and Fixes (3)
  • xamarin (5)
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About
    • Resume