How implement serilog in dot net core
Serilog in .NET Core, here's a quick guide:
Steps to Implement Serilog in .NET Core:
Install Serilog NuGet Packages Run the following command in your project:
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.File
2. Configure Serilog in
Program.cs
Add the following code to set up logging:using Serilog;var builder = WebApplication.CreateBuilder(args);// Configure SerilogLog.Logger = new LoggerConfiguration().WriteTo.Console().WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day).CreateLogger();builder.Host.UseSerilog();var app = builder.Build();app.Run();
3. Use Logging in Your Application
Inject
ILogger<T> into your services or controllers:public class MyService{private readonly ILogger<MyService> _logger;public MyService(ILogger<MyService> logger){_logger = logger;}public void DoSomething(){_logger.LogInformation("This is a log message from MyService.");}}
Comments
Post a Comment