c# - ASP.NET-Core 应用程序根目录中的 NLog 日志文件

标签 c# nlog asp.net-core-3.1

我的 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"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="basedir" value="c:\Users\user\source\repos\projectname\projectname"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>

<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->


<!--write events to a file with the date in the filename.-->
<target xsi:type="file" name="f" filename="${basedir}/logs/${shortdate}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />

</targets>

<rules>
<!-- add your logging rules here -->


 <!--Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"-->
 <logger name="*" minlevel="Debug" writeTo="f" />

 </rules>
</nlog>

它正在正常工作并按预期记录错误。

但是,我担心的是,当应用程序在线托管时,我如何才能将日志文件夹放在我的应用程序根目录中,因为我可能没有应用程序根目录的物理路径在我的目录中的拼写方式本地机器。

我尝试将以下行替换为 follws:

 <variable name="basedir" value="c:\Users\user\source\repos\projectname\projectname"/>

替换为

 <variable name="basedir" value="~/" />

没用。

我也试过如下替换下面这行

<target xsi:type="file" name="f" filename="${basedir}/logs/${shortdate}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />

替换为

<target xsi:type="file" name="f" filename="${Server.MapPath(~)}/logs/${shortdate}.log"
        layout="${longdate} ${uppercase:${level}} ${message}" />

但它不起作用。

无论应用程序的托管位置如何,请帮助指导我如何将日志文件夹置于应用程序根目录。

谢谢

最佳答案

你可以考虑使用这些:

对于 internalLogFile="..."那么你仅限于这些:%appdata% , ${basedir} , ${processdir} , ${currentdir} , ${tempdir} .

但您始终可以在运行时分配您自己的自定义路径。像这样:

<variable name="LogDirectory" value="${gdc:AppDirectory:whenEmpty=${basedir}}" />

<targets>
   <target type="file" filename="${LogDirectory}/Hello.txt" />
</targets>

然后在应用程序启动时在运行时执行此操作:

NLog.GlobalDiagnosticsContext.Set("AppDirectory", @"C:\Temp\");
NLog.Common.InternalLogger.LogFile = @"C:\Temp\nlog-internal.log";

关于c# - ASP.NET-Core 应用程序根目录中的 NLog 日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63749938/

相关文章:

c# - ASP.NET 存储交叉请求数据

c# - 如何使用 Xamarin 更改 ActionBar 菜单项的 Action View

c# - 在使用 asp.net 尝试登录 5 次失败后,如何将用户帐户锁定 30 分钟?

c# - LINQ 到 SQL : Updating without Refresh when “UpdateCheck = Never”

api - .NET Core 3.1 API - 307 临时重定向

c# - Razor 代码块在 ASP.NET Core 3.1 中不起作用

c# - 当调用 Flush() 时,NLog 是否应该刷新 AsyncTargetWrapper 中所有排队的消息?

c# - NLog多实例,如何?

c# - NLog - 在多个数据库列中保存 ${message}

azure-active-directory - Microsoft Identity Web - 如何获取用户登录事件?