C# ASP.NET-Core应用程序根目录中的NLog日志文件
我的nlog.config文件中有以下内容C# ASP.NET-Core应用程序根目录中的NLog日志文件,c#,nlog,asp.net-core-3.1,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=&
<?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>
它正在按预期工作并记录错误
但是,我担心的是,当应用程序联机托管时,如何使logs文件夹位于我的应用程序根目录中,因为我可能没有像在本地计算机上那样详细说明应用程序根目录的物理路径
我已尝试替换以下行,如下所示:
<variable name="basedir" value="c:\Users\user\source\repos\projectname\projectname"/>
替换为
<variable name="basedir" value="~/" />
<target xsi:type="file" name="f" filename="${Server.MapPath(~)}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
它不起作用
我还尝试替换以下行,如下所示
<target xsi:type="file" name="f" filename="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
替换为
<variable name="basedir" value="~/" />
<target xsi:type="file" name="f" filename="${Server.MapPath(~)}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
但它不起作用
请帮助指导我如何使日志文件夹位于应用程序根目录,而不考虑应用程序的托管位置
谢谢如果没有为日志文件提供特定路径,日志框架默认情况下会将日志文件创建到应用程序运行的同一位置 因此,如果希望在应用程序的根文件夹中生成日志文件,只需提供相对文件路径 在您的情况下,需要按照以下方式配置文件路径
internalLogFile="nlog-internal.log"
使用上面的行nlog internal.log
将在应用程序根文件夹中创建文件
及
在上行中,将在应用程序根文件夹中创建一个新文件夹logs
,并在logs
文件夹中创建名为{shortdate}的日志文件
我希望这将帮助您解决这个问题。如果没有为日志文件提供特定路径,默认情况下,日志框架会将日志文件创建到应用程序运行的同一位置 因此,如果希望在应用程序的根文件夹中生成日志文件,只需提供相对文件路径 在您的情况下,需要按照以下方式配置文件路径
internalLogFile="nlog-internal.log"
使用上面的行nlog internal.log
将在应用程序根文件夹中创建文件
及
在上行中,将在应用程序根文件夹中创建一个新文件夹logs
,并在logs
文件夹中创建名为{shortdate}的日志文件
我希望这能帮助你解决这个问题。 < P>你可以考虑使用这些:
- from(请记住包含在
中)
internalLogFile=“…”
,则仅限于以下内容:%appdata%
,${basedir}
,${processdir}
,${currentdir}
,${tempdir}
但您始终可以在运行时指定自己的自定义路径。像这样:
<variable name="LogDirectory" value="${gdc:AppDirectory:whenEmpty=${basedir}}" />
<targets>
<target type="file" filename="${LogDirectory}/Hello.txt" />
</targets>
<>你可以考虑使用这些:
- from(请记住包含在
中)
internalLogFile=“…”
,则仅限于以下内容:%appdata%
,${basedir}
,${processdir}
,${currentdir}
,${tempdir}
但您始终可以在运行时指定自己的自定义路径。像这样:
<variable name="LogDirectory" value="${gdc:AppDirectory:whenEmpty=${basedir}}" />
<targets>
<target type="file" filename="${LogDirectory}/Hello.txt" />
</targets>
如果执行
filename=“logs/${shortdate}.log”
如果执行filename=“logs/${shortdate}.log”