简体   繁体   English

Serilog:如何记录,将具有不同“来源”的消息记录到不同的文件

[英]Serilog: How to log, log message with different "origins" to different files

I want to log Information - Fatal with Serilog in my ASP.NET Core Web Application, using .NET Core 3.0我想在我的 ASP.NET Core Web 应用程序中使用 .NET Core 3.0 记录 Information - Fatal with Serilog

I want to log this data in different files (which was already asked and answered but my case is a little different I think):我想将这些数据记录在不同的文件中(已经询问并回答了,但我认为我的情况有点不同):

  • information.log => information about what the application did (only using the automatic logging of Serilog / Microsoft) information.log => 关于应用程序做了什么的信息(仅使用 Serilog / Microsoft 的自动日志记录)
  • error.log => All errors / unhandled exceptions that happen error.log => 发生的所有错误/未处理的异常
  • other_information.log => (don't really have a name for this one) information about the program BUT only log messages created by me other_information.log =>(实际上没有这个名字)关于程序的信息但只记录我创建的消息

I just can't figure out how to distinguish between information logs from Microsoft and my own.我只是不知道如何区分来自 Microsoft 和我自己的信息日志。

I have found a question / answer on how to disable logging from Serilog / Microsoft but doing so it deactivates it for all LogWriters, How to turn off the logging done by the ASP.NET core framework .我找到了一个关于如何从 Serilog/Microsoft 禁用日志记录的问题/答案,但这样做会为所有 LogWriter 停用它,如何关闭 ASP.NET 核心框架完成的日志记录

Then I found an answer to another question which maybe is exactly what I'm looking for, but I don't understand what happens so I prefer to not use it, because I don't like magic because if there is a problem, fixing it is a lot more time-consuming, Can I log to separate files using Serilog?然后我找到了另一个问题的答案,这可能正是我正在寻找的,但我不明白会发生什么,所以我宁愿不使用它,因为我不喜欢魔法,因为如果有问题,修复它更耗时,我可以使用 Serilog 登录到单独的文件吗? . .

I also found another question that looks similar: Serilog : Log to different files but I don't understand this one either.我还发现了另一个看起来类似的问题: Serilog : Log to different files但我也不明白这个。

So if one of those really is the solution to my Problem I would be grateful if someone could explain it to me.因此,如果其中之一确实是我的问题的解决方案,如果有人能向我解释,我将不胜感激。

This is the Logger Configuration that I have until now, all it does is log all Information in one file and all errors in the other one.这是我到目前为止所拥有的记录器配置,它所做的只是将所有信息记录在一个文件中,并将所有错误记录在另一个文件中。

            using Serilog;
            using Serilog.Events;            

            ...

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.Console()
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(e => e
                    .Level == LogEventLevel.Information)
                    .WriteTo.File(@"Logs\Info.log"))
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(e => e
                    .Level == LogEventLevel.Error)
                    .WriteTo.File(@"Logs\Error.log"))
                .CreateLogger();

And as I said, this works just fine to log all the information in one file and all the Errors in the other, but I don't know how to continue.正如我所说,这可以很好地将所有信息记录在一个文件中,并将所有错误记录在另一个文件中,但我不知道如何继续。

EDIT: I found the configurations I have at the moment from an answer to this question: Serilog - multiple log files ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌编辑:我从这个问题的答案中找到了我目前的配置: Serilog - 多个日志文件

我相信你只需要根据属性 SourceContext 过滤你的日志,这在这里解释: 根据上下文源过滤 Serilog 日志到不同的接收器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM