سؤال

I am using NLog and would like to use a non-standard directory for logging. My application, during load, will load a 'LogFileLocation' parameter which stores the location of the logfile directory.

Is it possible to use a non-standard logfile directory? And, if so, can I also update that in runtime if I have set autoReload="true"?

هل كانت مفيدة؟

المحلول

Firstly I would just leave the log files for any part of an application in the default location by convention, as that is where other developers will expect it.

I've done something similar with vb.net "Setting" within a WinForms ApplicationEvents to pull a value from the user specific values with mixed results (application upgrades caused issues). At the end of the day that logic doesn't translate well to C#, if that's your kettle of fish.

I assume that you have been through the app.config Nlog config settings like the following. I'll assume you can just hardcode the value at the parent app.config level to make it apply to both admin and service components of your application.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="logDirectory"
          value="${basedir}/logs/${date:format=yyyyMMdd}/"/>
<targets async="true">
  <target name="AppFullLog"
          layout="${longdate}    ${level:uppercase=true}     ${logger} on ${machinename} by ${windows-identity}.${newline}${message}${newline}Stack:${stacktrace}${newline}-------"
          xsi:type="File"
          fileName="${logDirectory}CadiFull.log">
  </target>
  <target name="AppErrorLog"
          layout="${longdate}    ${level:uppercase=true}     ${logger} on ${machinename} by ${windows-identity}.${newline}${message}${newline}Stack:${stacktrace}${newline}-------"
          xsi:type="File"
          fileName="${logDirectory}CadiErrors.log">
  </target>
</targets>
<rules>
  <logger name="*"
          writeTo="AppFullLog" />
  <logger name="*"
          levels="Error,Fatal"
          writeTo="AppErrorLog" />
</rules>

Finally, if your trying to get all your error logs into one location, you could always send the data to a server database. Nlog provides options to achieve that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top