简体   繁体   中英

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

I have the following in my nlog.config file

<?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.

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.

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} .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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