how to declare serilog in program.cs
how to use serilog in appsetting.json
how to use serilog dependency inject in controller class
how to create separate cls and interface for serilog for customize method
how to declare serilog in program.cs
how to use serilog in appsetting.json
how to use serilog dependency inject in controller class
how to create separate cls and interface for serilog for customize method
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Apr 20, 2023, 4:06 AM
Serilog is a popular logging framework for .NET applications. Here are the steps to declare Serilog in Program.cs, use it in appsettings.json, inject it into a controller class, and create separate classes and interfaces for Serilog to customize its methods:
Declare Serilog in Program.cs: In the Main method of Program.cs, you can configure Serilog by calling the UseSerilog method on the IWebHostBuilder instance. Here's an example:
Use Serilog in appsettings.json: You can use Serilog in appsettings.json by adding a "Serilog" section and configuring it with the desired sink(s) and minimum log level. Here's an example:
Use Serilog dependency injection in a controller class: You can use Serilog in a controller class by adding a constructor parameter of type ILogger (where T is the name of your controller class). The ILogger instance will be automatically injected by the ASP.NET Core dependency injection system. Here's an example:
Create a separate class and interface for Serilog to customize its methods: You can create a separate class and interface for Serilog to customize its methods by implementing the ILogger interface and passing it to the ILoggerFactory. Here's an example:
You can then register the MyLogger class and interface with the ASP.NET Core dependency injection system in the Startup.cs file like this:
And then use the IMyLogger interface in a controller like this:
Naimish MakwanaPosted Apr 20, 2023, 3:56 AM
Hello Meghana,
Please refer below links:
https://www.codeproject.com/Articles/5344667/Logging-with-Serilog-in-ASP-NET-Core-Web-API#:~:text=Create%20ASP.NET%20Core%20Web%20API%20Project&text=Choose%20.,AspNetCore%20.
https://codingsonata.com/logging-with-serilog-in-asp-net-core-web-api/
https://procodeguide.com/programming/aspnet-core-logging-with-serilog/
Thanks
Naimish Makwana
Jignesh KumarPosted Apr 20, 2023, 3:32 AM
Hello Meghana,
You can refer this thread,
https://www.c-sharpcorner.com/article/serilog-in-asp-net-core-3-1/
https://blog.christian-schou.dk/use-serilog-with-asp-net-core-net6/