【问题标题】:How to listen output logs in ASP.NET Core 2.2如何在 ASP.NET Core 2.2 中监听输出日志
【发布时间】:2019-06-26 20:19:30
【问题描述】:

我正在使用 ASP.NET Core 2.2 开发 API 服务器,我的服务器机器是 Ubuntu 18.04。

我有一个关于我的 API 状态的日志文件,但我觉得缺少 ASP.NET(Kestrel) 状态信息,例如路由结果到我的控制器。

ASP.NET 生成如下日志。

ServerApplication> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
ServerApplication>       Request starting HTTP/1.1 GET  
                         http://localhost:57904/api  
ServerApplication> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
ServerApplication>       Request starting HTTP/1.1 DEBUG
                         http://localhost:57904/  0
ServerApplication> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
ServerApplication>       Request finished in 36.827ms 200 
ServerApplication> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]

我想收听或捕获这些日志以放入我的日志文件。

我读过this article,但似乎不是在谈论听日志。

如何解决这个问题?

【问题讨论】:

  • 您可以试用 Serilog ,它为您提供了多种选项来丰富和记录不同格式和目的地的事件。
  • @SoumenMukherjee 我有自己的日志记录 API,我不觉得这很糟糕。我只想知道 Kestrel 的状态。不记录 API。
  • 所以我看到您编写了自己的自定义事件接收器.. 好..

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

我刚刚调了Serilog.Extensions.Logging.File

using Serilog;
using Serilog.Core;
using Serilog.Events;

public void Configure(IApplicationBuilder app,
                      IHostingEnvironment env,
                      ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog(
        new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.Sink(new LogEventSink())
            .CreateLogger());
}

public class LogEventSink : ILogEventSink
{
    public void Emit(LogEvent logEvent)
    {
        string message = logEvent.RenderMessage();
        // Write to my log.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2017-11-16
    • 2022-01-01
    相关资源
    最近更新 更多