使用Serilog

时间:2016-08-04 06:53:19

标签: c# logging serilog

我正在尝试使用我的ASP.NET Core应用程序设置Serilog。我想为所有控制器提供一个日志文件,一个用于所有服务,一个用于其余服务,理想情况下包含一切。每个控制器都继承BaseController和每个服务BaseService。我正在调用的控制器和服务正在编写跟踪日志事件。

通过依赖注入检索记录器和服务。该服务看起来像控制器(关于记录器)。

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 (仅与基础合作伙伴合作)

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();

这将仅为“other”和“all”创建日志文件。两者都包含来自控制器和服务的日志事件。

方法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();

这将仅为“other”和“all”创建日志文件。 “all”包含来自控制器和服务的日志事件。

方法3 (仅限用户类)

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();

这会创建所有日志文件,每个文件都包含预期的消息。

为实现预期目标需要做些什么(见本文第二句)。

祝你好运,    卡斯滕

1 个答案:

答案 0 :(得分:11)

Serilog也会按照您描述的方式通过命名空间过滤来执行此操作:

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函数来代替isControllerisService来识别它们。

(您的方案可能更适合于允许更轻松过滤的日志记录格式,因此您可以通过在之后过滤来有选择地查看控制器事件,服务事件等等。其他Serilog provided sinks用于某些选项。)