【问题标题】:Serilog not logging when published ASP.NET core发布 ASP.NET 核心时 Serilog 不记录
【发布时间】:2019-06-05 03:30:42
【问题描述】:

我已将 Serilog 配置为在具有 ASP.NET 核心(运行 Core 2.1)的 WebApi 上同时写入 MSSQL 表和文件(以尝试确定发生了什么。)。

我在startup.cs 中添加 Serilog 日志记录如下;

public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var log = new LoggerConfiguration()
        .MinimumLevel.Verbose()
        .WriteTo.MSSqlServer(
            DbGlobals.DevDatabase, "Logs", schemaName:DbGlobals.SchemaName)
        .WriteTo.File("Logs\\Serilog\\log.txt", rollingInterval: RollingInterval.Day)
        .CreateLogger();

    loggerFactory.AddSerilog(log);

其中DbGlobals.DevDatbase 和Dbglobals.SchemaName 是从配置中读取的字符串。

更新这些值作为参考;

DbGlobals.Schema = "MySchema";
DbGlobals.DevDatabase = "Data Source=xx.xx.xx.xx;Initial Catalog=DevDatabase;Integrated Security=False;Persist Security Info=False;User ID=LOGIN;Password=PASSWORD;MultipleActiveResultSets=true;";

当使用 IIS Express 在本地运行并在 debug 和 release 配置下运行时,两个日志记录功能都可以正常工作。例如在我的控制器中;

public class ReviewPublicController : Controller
{
    private readonly IReviewService _service;
    private readonly IMapper _mapper;
    private readonly IMailService _mailService;
    private readonly ILogger _logger;

    public ReviewController(
        IReviewService service, 
        ILogger<ReviewController> logger)
    {
        _service = service;
        _logger = logger;
    }

    [HttpPost]
    [ProducesResponseType(typeof(Review), 201)]
    [Route("")]
    public async Task<IActionResult> Post([FromBody]ReviewPostModel model)
    {
        _logger.LogDebug("Posting Review");

        ...

        return Ok();
    }

所以本地“发布评论”被写入我的文件和 DbSchema。

但是,当我发布到我的服务器时,上面的“发布评论”条目也不会写入。

两者似乎都在工作,因为正在从 EF 和路由等添加信息条目,但当我专门尝试编写条目时却没有?

谁能告诉我这里可能是什么问题?

当前的 nuget 版本是;

Serilog 2.7.1
Serilog.AspNetCore 2.1.1
Serilog.Sinks.MSSqlServer 5.1.2
Serilog.Sinks.File 4.0.0

另外,我的appsettings.json 中有以下内容;

  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Release": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }

【问题讨论】:

  • 请分享DbGlobals.DevDatabase
  • serilog 有 appsetting.json 吗?
  • 好的我已经更新了,虽然不确定相关性,因为我可以确认它连接到数据库。它还写入文件而不是我的日志输出
  • @Stefan appsettings.json 中有默认的日志记录部分,但是(除非我错了)上面没有考虑到这些? (这是我确认日志记录正常工作后的下一步)。我现在补充一下

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


【解决方案1】:

更新

如文档中所述:

默认级别 - 如果未指定最小级别,则将处理信息级别及更高级别的事件。

所以基本上,您的配置似乎无效或被覆盖。见:https://github.com/serilog/serilog/wiki/Configuration-Basics


LogDebug 在 Release 构建中默认禁用,这可能是您发布的内容。

请尝试

_logger.LogInformation("Posting Review");

【讨论】:

  • 听起来很有希望,我现在就试一试。
  • 这有效,我可以确认LogInformation 和LogError 在发布时都有效。奇怪的是它在本地发布版本下工作,我认为LogDebug 只是日志记录层次结构的一部分?你知道为什么不是这样吗? (出于兴趣)
  • 这很有趣,我一直认为这是由日志记录配置设置控制的。
  • 嗯,你说得对,就像在文档中一样:Default Level - if no MinimumLevel is specified, then Information level events and higher will be processed.。所以基本上,您的配置似乎无效或被覆盖。见:github.com/serilog/serilog/wiki/Configuration-Basics
  • 是的,这些是我关注的文档。这就是我使用.MinimumLevel.Verbose() 的原因,就像你说的 WebDeploy 可能会导致变化??
【解决方案2】:

对我来说,解决方案是从 appsettings.json 中删除 "restrictedToMinimumLevel": "Information"。

在appsettings.Development.json 中完全一样,并且在开发环境中运行良好。

【讨论】:

    【解决方案3】:

    对我来说,我已经配置了以上所有内容。它在我的本地环境中记录 db,因为它在开发环境中不起作用。问题是由于身份验证。我正在为我的应用程序使用 azure 广告身份验证,它限制 serilog 写入服务器。因此,在我的 program.cs 文件中,我添加了以下设置。

    程序.cs

    var sqlSinkOptions = new MSSqlServerSinkOptions
    {
          TableName = "MyLogTableName",
          UseAzureManagedIdentity = true,  // This line
          AzureServiceTokenProviderResource = "https://database.windows.net/" // This line
    };
    

    之后,我可以看到开发环境中也发生了日志记录。可能这对某人有帮助。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多