简体   繁体   English

如何使用 NLog 在 Linux 上的 var/log/my-app-dir 中添加日志文件和文件夹

[英]How to add log files and folder in var/log/my-app-dir on Linux with NLog

I'm trying to add logging to a WebAPI in .NET core via NLog, but can't seem to get Linux to let me write log files to var/log/my-app .我正在尝试通过 NLog 将日志记录添加到 .NET 核心中的 WebAPI,但似乎无法让 Linux 让我将日志文件写入var/log/my-app I can't find any documentation other than the extension for SysLog but this doesn't fit me needs.除了SysLog的扩展之外,我找不到任何文档,但这不符合我的需要。 The goal is to define a folder named after the app and store the internal, all, and own files there like so:目标是定义一个以应用程序命名的文件夹,并将内部文件、所有文件和自己的文件存储在那里,如下所示:

var
|-- log
    |-- my-app
        |-- internal.txt
        |-- nlog-all.log
        |-- nlog-own.log

I could be missing docs or something else, but I haven't found how to specify more than just writing to the system logs via SysLog.我可能缺少文档或其他东西,但我还没有找到如何指定不仅仅是通过 SysLog 写入系统日志。 How should one go about doing this with NLog in a Linux environment like Ubuntu?在像 Ubuntu 这样的 Linux 环境中,应该如何使用 NLog 做到这一点?

The NLog InternalLogger is mostly for diagnostics and troubleshooting, so the logic for dynamic control of logging path is rather limited for simplicity and reduced chance of errors. NLog InternalLogger 主要用于诊断和故障排除,因此日志路径的动态控制逻辑相当有限,以简化和减少出错的机会。

You can right now use the following keywords:您现在可以使用以下关键字:

  • ${basedir} ${basedir}
  • ${processdir} ${进程目录}
  • ${currentdir} ${当前目录}
  • ${tempdir} ${临时目录}
  • %appdata% %应用程序数据%

See also: https://github.com/NLog/NLog/wiki/Internal-Logging另见: https : //github.com/NLog/NLog/wiki/Internal-Logging

In your case then I would probably consider perform a token-search-replace on deployment of NLog.config (Make a search for XML-transformations on deployment).在您的情况下,我可能会考虑在部署NLog.config执行令牌搜索替换(在部署时搜索 XML 转换)。

Alternative just assign the application-name at startup like this:另一种方法是在启动时分配应用程序名称,如下所示:

var logDir = "var/log/my-app";
NLog.GlobalDiagnosticsContext.Set("LogDir", logDir);
NLog.Common.InternalLogger.LogFile = System.IO.Path.Combine(logDir, "internal.txt");

With this NLog.config:使用此 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"
      throwConfigExceptions="true"
      internalLogLevel="info">

  <targets>
    <target xsi:type="File" name="allfile" fileName="${gdc:LogDir}/nlog-all.log"  />
    <target xsi:type="File" name="ownfile" fileName="${gdc:LogDir}/nlog-own.log" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" />  <!-- BlackHole -->

    <logger name="*" minlevel="Trace" writeTo="ownfile" />
  </rules>
</nlog>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM