简体   繁体   English

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

[英]NLog log files in ASP.NET-Core Application Root Directory

I have the following in my nlog.config file我的 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>

and it is working and loging the errors as expected.它正在按预期工作并记录错误。

However, my concern is that when the application gets hosted online, How can I make the logs folder to be at my application root directory since I might not have the physical path to the application root spelt out the way it is on my local machine.但是,我担心的是,当应用程序在线托管时,如何使日志文件夹位于我的应用程序根目录中,因为我可能没有按照本地计算机上的方式说明应用程序根目录的物理路径。

I have tried replacing the following line as follws:我尝试替换以下行如下:

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

replaced with替换为

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

It did not work.这没用。

I have also tried replacing the following line as follows我也试过如下替换以下行

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

replaced with替换为

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

But it is not working.但它不起作用。

Please help to guide me on how to get the logs folder to be at the application root regardless of the hosting location of the application.请帮助指导我如何使日志文件夹位于应用程序根目录,而不管应用程序的托管位置如何。

Thank you谢谢

If specific paths are not provided for log files, logging frameworks by default create log files to the same location where the application runs.如果未为日志文件提供特定路径,默认情况下,日志记录框架会将日志文件创建到应用程序运行的同一位置。

So if you want the log files to be generated at root folder of the application, you just need to provide relative file path.因此,如果您希望在应用程序的根文件夹中生成日志文件,您只需要提供相对文件路径。

In your case you need to have file paths configured as following.在您的情况下,您需要按如下方式配置文件路径。

internalLogFile="nlog-internal.log"

With the above line nlog-internal.log file will be created in the application root folder.使用上面的行nlog-internal.log文件将在应用程序根文件夹中创建。

and

filename="logs/${shortdate}.log"

With the above line a new folder logs will be created in the application root folder and the log file with {shortdate} name will be created in logs folder.使用上面的行,将在应用程序根文件夹中创建一个新文件夹logs ,并在日志文件夹中创建名称为 {shortdate} 的logs文件。

I hope this will help you solve the issue.我希望这会帮助您解决问题。

You can consider using these:您可以考虑使用这些:

For internalLogFile="..." then you are restricted to these only: %appdata% , ${basedir} , ${processdir} , ${currentdir} , ${tempdir} .对于internalLogFile="..."那么你仅限于这些: %appdata% , ${basedir} , ${processdir} , ${currentdir} , ${tempdir}

But you can always assign you own custom path at runtime.但是您始终可以在运行时分配自己的自定义路径。 Like this:像这样:

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

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

Then do this at runtime at app-startup:然后在应用程序启动时在运行时执行此操作:

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

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

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