如何在asp.net core中设置linux nlog文件夹

问题描述 投票:0回答:2

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

我的 nlog.config 片段如下所示,

<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";

没用。任何人都可以帮忙吗?如何将日志文件存储在 ar\log 文件夹中?

谢谢

c# linux asp.net-core nlog
2个回答
2
投票

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

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

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();
© www.soinside.com 2019 - 2024. All rights reserved.