Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Serilog记录器配置(按SourceContext)_C#_Logging_Asp.net Core_Serilog - Fatal编程技术网

C# Serilog记录器配置(按SourceContext)

C# Serilog记录器配置(按SourceContext),c#,logging,asp.net-core,serilog,C#,Logging,Asp.net Core,Serilog,我试图在一个小样本项目中实现serilog。到目前为止,我已经做了以下工作: 创建新的ASP.NET核心Web应用程序(MVC) 安装Serilog.AspNetCore软件包 安装程序包Serilog.Sinks.File 安装程序包Serilog.Formatting.Compact 关注博客帖子 试着关注博客帖子 我的问题: 是否有可能根据CategoryName(在我的例子中是“MyLogger”)配置两个不同的记录器?我希望此记录器使用与默认记录器不同的文件。在我的快乐世界幻想中,

我试图在一个小样本项目中实现serilog。到目前为止,我已经做了以下工作:

  • 创建新的ASP.NET核心Web应用程序(MVC)
  • 安装Serilog.AspNetCore软件包
  • 安装程序包Serilog.Sinks.File
  • 安装程序包Serilog.Formatting.Compact
  • 关注博客帖子
  • 试着关注博客帖子
我的问题:

是否有可能根据CategoryName(在我的例子中是“MyLogger”)配置两个不同的记录器?我希望此记录器使用与默认记录器不同的文件。在我的快乐世界幻想中,它看起来像:

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.File(new CompactJsonFormatter(), @"C:\temp\standardLog.txt")
            .WriteTo.File(new CompactJsonFormatter(), @"C:\temp\specialLog.txt").Filter.ByIncludingOnly(x => x.SourceContext("MySpecialLogger")
            .CreateLogger();
如果我创建一个新的记录器
var Logger=\u loggerFactory.CreateLogger(“MySpecialLogger”)日志将安全保存在我的specialLog.txt文件中

有什么想法吗

您需要Serilog.Sinks.Map来完成此操作

dotnet add <PROJECT> package Serilog.Sinks.Map -v 1.0.0-dev-00012

首先:谢谢你有时间回答我的问题!!关于您的答案:条件运算符失败(仅编译器错误赋值,call(…)可用作语句)。。此参数无效:
Action configure=(x,wt)=>x==“”?wt.File(“”):wt.File(“”
;-但是我真的很喜欢地图的方法。。还有别的主意吗?太好了!如果我使用RollingFile Sink,我就有我想要的一切:)非常感谢!!!但仍有一些意想不到的行为:MySpecialLogger按预期工作。但是如果我使用文件接收器,我的StandardLogger只输出“启动Web主机”。如果我使用RollingFile,我会得到两个日志文件(standard-{date}.txt和standard-{date}\u 001.txt),其中001日志包含我的消息,而另一个只包含启动消息。如果我删除
Log.Information(“启动web主机”)
-调用,一切都会正常工作……啊!对不起,答案不是100%正确,我会更新的。
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
        .Enrich.FromLogContext()
        .WriteTo.Map("SourceContext", null, (sc, wt) =>
            wt.File(new CompactJsonFormatter(), sc == "MySpecialLogger" ?
                @"C:\temp\specialLog.txt" :
                @"C:\temp\standardLog.txt"))
        .CreateLogger();