【问题标题】:How to set linux nlog folder in asp.net core如何在 asp.net core 中设置 linux nlog 文件夹
【发布时间】:2021-05-13 18:47:16
【问题描述】:

我在我的 asp.net core 2.0 应用程序中使用 NLog,我想将它部署到 Ubuntu。我可以找到很多线程和文章,但它们都部署在 Windows 中。

我的 nlog.config sn-p 如下所示,

<target xsi:type="File" name="ownlog" fileName="${var:configDir}\nlog-own.log"
             layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />

在我的 Startup.cs 文件中,我将 configDir 设置如下,

LogManager.Configuration.Variables["configDir"] = "\\var\\log";

它不起作用。任何人都可以帮忙吗?如何将日志文件存储在 \var\log 文件夹中?

谢谢

【问题讨论】:

  • 您是否尝试过反转斜线,使它们变成 unix 样式: /var/log/nlog-own.log ?您是否尝试过检查文件权限?你检查过 NLog InternalLogger 吗? (github.com/NLog/NLog/wiki/Internal-Logging)

标签: c# linux asp.net-core nlog


【解决方案1】:

您必须将路径从双反斜杠更改为单斜杠。如果你想保留这两种环境,你可以这样做:

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
// use windows path
else
// use linux path

【讨论】:

  • 请提供一个恰当的例子。这段代码只是展示了如何在 .net core 中获取 os 平台,而不是如何在 nlog 中使用它。
【解决方案2】:

我使用了 NLog 默认示例,并将 nlog.config 中的反斜杠替换为正斜杠在 windows 和 linux docker 中都有效。 Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) 获取我的{basedir} 也适用于两者。

<target xsi:type="File" name="allfile" fileName="${basedir}/logs/all-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
var name = "all";
var entryDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var baseDir = new DirectoryInfo(Path.Combine(entryDir, @"logs"));
var logFilePath = baseDir.GetFiles()
                    .Where(f => f.Name.Contains(name))
                    .OrderByDescending(f => f.LastWriteTime)
                    .First();

【讨论】:

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