我被要求写日志(log)级别的信息,警告和开发中的错误,但每一个都在一个单独的文件中,所以我有information.txt只包含信息日志(log),warning.txt只包含警告日志(log),Error.txt只包含错误日志(log).

这是我目前的实现(它起作用了).

Program.cs年中:

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

在应用程序设置中:

"Serilog": {
  "MinimumLevel": "Information",
  "WriteTo": [
    {
      "Name": "Console"
    },
    {
      "Name": "File",
      "Args": {
        "path": "wwwroot/Logs/log.txt",
        "rollingInterval": "Day",
        "reatinedFileCountLimit": 14,
        "restrictedToMinimumLevel": "Information"
      }
    }
  ]
},

我已经安装了Serilog.Expressions包并阅读了文档,我知道我必须使用这样的东西

"Filters": [
  {
    "Name": "ByIncludingOnly",
    "Args": {
      "expression": "@l = 'Information'"
    }
  }
]

但我不明白它应该放在哪里.我试着把它放在ArgsArgsName之间,但都不起作用,有人能帮我吗?

我已经在Stackoverflow上阅读了很多帖子,但都没有奏效:(

推荐答案

这可以工作,但不能使用文档.

using Serilog;
using Serilog.Events;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Logger(l => l
        .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information)
        .WriteTo.File(
            path: "information.txt"))

    .WriteTo.Logger(l => l
        .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
        .WriteTo.File(
            path: "error.txt"))
        .CreateLogger();

Log.Information("test Information");
Log.Error("test Error");

-使用appsettings.json更新-
安装程序包Serilog.Expressions

  "Serilog": {
    "WriteTo": [
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "@l = 'Information'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "information.txt"
                }
              }
            ]
          }
        }
      },
      {
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "@l = 'Error'"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "error.txt"
                }
              }
            ]
          }
        }
      }
    ]
  }

Test Result
enter image description here

enter image description here

Csharp相关问答推荐

更新数据库中的对象失败,原因是:Microsoft. EntityFrame Core. GbUpdateConcurrencyResponse'

.NET框架4.7.2项目如何引用.NET Core 2.2库?

EF Core Fluent API中定义的多对多关系

EF Core判断是否应用了AsSplitQuery()

为什么总输出就像12.3没有一分一样?

在C#中,DirectoryEntry返回空AuditRules集合,即使审计规则确实存在

使用两个不同的枚举作为Switch语句中的CASE生成唯一值

在C#中,非静态接口方法的抽象和虚拟是冗余的吗?

.NET 8 DI GetServices<;对象&>不工作

带有列表参数的表达式树

升级后发出SWITCH语句

C#动态设置ServerReport报表参数

.Net MAUI,在将FlyoutPage添加到容器之前,必须设置添加构造函数导致Flyout和Detail"

C#命名管道-编码错误?

无法向Unity注册Microsoft Logger

为什么我的UserControl没有加载到我的主窗口中?

S,在.NET核心控制台应用程序中,AddScope和AddSingleton有什么不同?

.NET6最小API:操作.MapGet之后的响应

是否在异步方法中避免Span<;T>;.ToArray()?

XmlSerializer在遇到XML属性(命名空间)时崩溃