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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Apr 20, 2023, 12:06 AM EDT
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:
using Serilog; using Serilog.Events; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); }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:
{ "Serilog": { "MinimumLevel": { "Default": "Debug", "Override": { "Microsoft": "Warning", "System": "Warning" } }, "WriteTo": [ { "Name": "Console" }, { "Name": "File", "Args": { "path": "log.txt", "rollingInterval": "Day" } } ] } }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:
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace MyWebApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index() { _logger.LogInformation("Index page visited."); return View(); } } }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:
using Serilog; using Microsoft.Extensions.Logging; using System; public interface IMyLogger { void Log(LogLevel logLevel, string message); } public class MyLogger : IMyLogger { private readonly ILogger _logger; public MyLogger() { _logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); } public void Log(LogLevel logLevel, string message) { switch (logLevel) { case LogLevel.Trace: _logger.Verbose(message); break; case LogLevel.Debug: _logger.Debug(message); break; case LogLevel.Information: _logger.Information(message); break; case LogLevel.Warning: _logger.Warning(message); break; case LogLevel.Error: _logger.Error(message); break; case LogLevel.Critical: _logger.Fatal(message); break; case LogLevel.None: break; default: throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null); } } }You can then register the MyLogger class and interface with the ASP.NET Core dependency injection system in the Startup.cs file like this:
public void ConfigureServices(IServiceCollection services) { services.AddSingleton(); services.AddControllers(); }And then use the IMyLogger interface in a controller like this:
public class HomeController : Controller { private readonly IMyLogger _logger; public HomeController(IMyLogger logger) { _logger = logger; } public IActionResult Index() { _logger.Log(LogLevel.Information, "Index page visited."); return View(); } }Jignesh KumarRank #91 · 22.9K reputationPosted Apr 19, 2023, 11:32 PM EDT
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/
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 19, 2023, 11:56 PM EDT
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