• 如何在.NET中使用Serlog日志组件
  • 发布于 1个月前
  • 53 热度
    0 评论
一、控制台项目
1.1、在项目中添加下面几个组件包
Install-Package Serilog.Extensions.Logging
Install-Package Serilog
Install-Package Serilog.Sinks.Console
Install-Package Serilog.Sinks.File
1.2、Program.cs代码参考如下
using System;
using Serilog;

class Program
{
    static async Task Main()
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();
//上面和下面的都可以        
//           Log.Logger = new LoggerConfiguration()
//          .MinimumLevel.Information()
//          .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
//#if DEBUG
//           .MinimumLevel.Override("Xxx", LogEventLevel.Debug)
//#else
//            .MinimumLevel.Override("Xxx", LogEventLevel.Information)
//#endif
//          .Enrich.FromLogContext()
//          .WriteTo.Console()
//          .WriteTo.File(Path.Combine(Directory.GetCurrentDirectory(), "logs/logs.txt"))
//          .CreateLogger();

        Log.Information("Hello, world!");

        int a = 10, b = 0;
        try
        {
            Log.Debug("Dividing {A} by {B}", a, b);
            Console.WriteLine(a / b);
        }
        catch (Exception ex)
        {
            Log.Error(ex, "Something went wrong");
        }
        finally
        {
            await Log.CloseAndFlushAsync();
        }
    }
}
二、AspNetCore项目
2.1、在项目中添加下面几个组件包
Install-Package Serilog.AspNetCore
Install-Package Serilog.Sinks.Async
Install-Package Serilog.Sinks.File
2.2、Program.cs代码参考:
using Serilog;
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

try
{
    Log.Information("Starting web application");
    var builder = WebApplication.CreateBuilder(args);
    builder.Host.UseSerilog(); // <-- Add this line
    var app = builder.Build();
    app.MapGet("/", () => "Hello World!");
    app.Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
    // 堆代码 duidaima.com
    Log.CloseAndFlush();
}
 //Serilog.Sinks.Http
 //Serilog.Sinks.Seq
 //Log.Logger = new LoggerConfiguration()
 //            .MinimumLevel.Verbose()
 //            .Enrich.WithProperty("ApplicationContext", Program.AppName)
 //            .Enrich.FromLogContext()
 //            .WriteTo.Console()
 //            .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
 //            .WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl)
 //            .ReadFrom.Configuration(configuration)
 //            .CreateLogger();
使用的的话直接注入ILogger就可以使用了。

参考文档:
官网:https://serilog.net/
官方文档:
控制台:https://github.com/serilog/serilog/wiki/Getting-Started
web:https://github.com/serilog/serilog-aspnetcore#serilogaspnetcore---

配置:Configuration Basics · serilog/serilog Wiki · GitHub
https://docs.meowv.com/stack/dotnetcore/serilog-in-dotnet.html
https://maomi.whuanle.cn/3.2.serilog.html
https://github.com/serilog/serilog-aspnetcore
https://www.cnblogs.com/ireadme/p/14509704.html
用户评论