使用Serilog的一个记录器的多个过滤器
我正在尝试使用我的ASP.NET Core应用程序设置Serilog.我想要一个用于所有控制器的日志文件,一个用于所有服务的日志文件,一个用于其他服务的日志文件,理想情况下是一个包含所有内容的日志文件.每个控制器都继承BaseController
和每个服务BaseService
.我正在调用的控制器和服务正在编写跟踪日志事件.
I am trying to setup Serilog with my ASP.NET Core application. I'd like to have one log file for all controllers, one for all services, one for the rest and ideally one which contains everything. Every controller is inheriting BaseController
and every service BaseService
. The controller and the service I am calling are writing a trace log event.
通过依赖注入检索记录器和服务.该服务看起来像控制器(关于记录器).
The logger and the service are retrieved via dependy injection. The service looks like the controller (regarding the logger).
public class UsersController: BaseController
{
private UserService service { get; }
public UsersController(ILogger<UsersController> logger, UserService userService) : base(logger)
{
service = userService;
}
}
public abstract class BaseController: Controller
{
protected readonly ILogger<BaseController> Logger;
public BaseController(ILogger<BaseController> logger)
{
Logger = logger;
}
}
方法1 (仅适用于基本分类)
Approach 1 (working with the base clases only)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Logger(l => l
.MinimumLevel.Verbose()
.WriteTo.Logger(l2 => l2
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<BaseController>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-controller-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<BaseService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-service-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByExcluding(Matching.FromSource<BaseController>())
.Filter.ByExcluding(Matching.FromSource<BaseService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-other-{Date}.log"))
)
.WriteTo.RollingFile("d:/logs/recon-api-all-{Date}.log"))
.CreateLogger();
这将仅为其他"和全部"创建日志文件.两者都包含来自控制器和服务的日志事件.
This creates the log files for "other" and "all" only. Both contain the log events from controller and service.
方法2 (适用于具体课程)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Logger(l => l
.MinimumLevel.Verbose()
.WriteTo.Logger(l2 => l2
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<BaseController>())
.Filter.ByIncludingOnly(Matching.FromSource<PrivilegeGroupsController>())
.Filter.ByIncludingOnly(Matching.FromSource<PrivilegesController>())
.Filter.ByIncludingOnly(Matching.FromSource<UsersController>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-controller-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<BaseService>())
.Filter.ByIncludingOnly(Matching.FromSource<PrivilegeGroupService>())
.Filter.ByIncludingOnly(Matching.FromSource<PrivilegeService>())
.Filter.ByIncludingOnly(Matching.FromSource<UserService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-service-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByExcluding(Matching.FromSource<BaseController>())
.Filter.ByExcluding(Matching.FromSource<UsersController>())
.Filter.ByExcluding(Matching.FromSource<PrivilegeGroupsController>())
.Filter.ByExcluding(Matching.FromSource<PrivilegesController>())
.Filter.ByExcluding(Matching.FromSource<BaseService>())
.Filter.ByExcluding(Matching.FromSource<UserService>())
.Filter.ByExcluding(Matching.FromSource<PrivilegeGroupService>())
.Filter.ByExcluding(Matching.FromSource<PrivilegeService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-other-{Date}.log"))
)
.WriteTo.RollingFile("d:/logs/recon-api-all-{Date}.log"))
.CreateLogger();
这将仅为其他"和全部"创建日志文件. 全部"包含来自控制器和服务的日志事件.
This creates the log files for "other" and "all" only. "all" contains the log events from controller and service.
方法3 (仅适用于用户类别)
Approach 3 (working with the user classes only)
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Logger(l => l
.MinimumLevel.Verbose()
.WriteTo.Logger(l2 => l2
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<UsersController>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-controller-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByIncludingOnly(Matching.FromSource<UserService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-service-{Date}.log"))
.WriteTo.Logger(l3 => l3
.Filter.ByExcluding(Matching.FromSource<UsersController>())
.Filter.ByExcluding(Matching.FromSource<UserService>())
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-other-{Date}.log"))
)
.WriteTo.RollingFile("d:/logs/recon-api-all-{Date}.log"))
.CreateLogger();
这将创建所有日志文件,并且每个文件都包含预期的消息.
This creates all the log files and every file contains the expected messages.
要实现期望的目标需要做什么(请参见本文的第二句话).
What needs to be done to achieve the desired goal (see second sentence of this post).
最诚挚的问候, 卡斯滕
Best regards, Carsten
Serilog还将按照您所描述的那样通过按名称空间过滤来做到这一点:
Serilog will also do this as you describe, by filtering by namespace:
var isController = Matching.FromSource("MyApp.Controllers");
var isService = Matching.FromSource("MyApp.Services");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.RollingFile("d:/logs/recon-api-all-{Date}.log")
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly(isController)
.WriteTo.RollingFile("d:/logs/recon-api-controller-{Date}.log"))
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly(isService)
.WriteTo.RollingFile("d:/logs/recon-api-service-{Date}.log"))
.WriteTo.Logger(l => l
.Filter.ByExcluding(e => isController(e) || iService(e))
.WriteTo.RollingFile("d:/logs/recon-api-other-{Date}.log"))
.CreateLogger();
如果无法通过名称空间识别控制器和服务,则可以编写lambda函数代替isController
或isService
来识别它们.
If the controllers and services aren't identifiable by namespace, you can write a lambda function in place of isController
or isService
to identify them.
(您的方案可能更适合于允许更轻松过滤的日志记录格式,因此您可以通过在事后过滤 来有选择地查看控制器事件,服务事件等.其他Serilog 提供的接收器.)
(Your scenario might be better suited to a logging format that permits easier filtering, so that you can selectively view controller events, service events and so-on by filtering after the fact. Check out the other Serilog provided sinks for some options.)