【问题标题】:Nlog does not work on linux docker containerNlog 在 linux docker 容器上不起作用
【发布时间】:2021-12-23 09:52:03
【问题描述】:

我有以下 nlog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="true"
      internalLogFile="../app/internal.log"
      internalLogToConsole="true"
      internalLogLevel="Trace" >
  <time type='AccurateUTC' />
  <variable name="filePath" value="../app/logs/${shortdate}.log" />

  <targets async="true">
    <target xsi:type="Console" name="logconsole"
            layout="${level}|${message}|${all-event-properties} ${exception:format=tostring}" />
    <target xsi:type="File" name="logfile" fileName="${filePath}"
            layout="${longdate}|${level}|${callsite}|${message}|${all-event-properties} ${exception:format=tostring}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile, logconsole" />
  </rules>
</nlog>

此日志在 Windows 上运行没有问题,但在容器上运行它没有任何作用。

也没有internal.log,所以不知道在哪里看。

我将 Nlog 记录器附加到应用程序,如下所示:

private static void ConfigureLogging(HostBuilderContext host, ILoggingBuilder loggingBuilder)
{
    loggingBuilder.ClearProviders();
    loggingBuilder.AddConsole();
    loggingBuilder.SetMinimumLevel(LogLevel.Trace);
    loggingBuilder.AddNLog(host.Configuration);
}

我尝试了多种方法,但到目前为止都没有运气。

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 您是否在docker-compose.yml 中配置了卷映射?您是否尝试过使用绝对路径而不是相对路径。前任。 /app/logs/nlog_internal.log ?
  • 是的!我也试过了:(

标签: c# docker nlog


【解决方案1】:

嗯, 问题出在 Dockerfile 订单执行中: 出于某种我还不知道的原因,当我打电话时:

运行 apt-get

运行 wget

以及之后的一些其他命令

FROM 基础 AS 最终

WORKDIR /app

复制 --from=publish /app/publish .

Nlog 无法找到正确的目录。 解决方案是在第一个 FROM 子句之后执行所有“RUN apk ...”管道。 我希望它可以帮助其他有类似情况的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    相关资源
    最近更新 更多