Nlog: how to write a config file for implementing that create a new log file named by longdate every app starting up

Created on 4 Jan 2020  Â·  4Comments  Â·  Source: NLog/NLog

I have tried to create a variable at config file
<variable name="fileName" value="${longdate}" />

<target name="info" keepFileOpen="true" xsi:type="File" fileName="${basedir}/logs/info/log_${fileName}.log"           
            encoding="utf-8" layout="${longdate} ${callsite} [${level}]: ${message}" />

but it create a lot log file. how to write a config file for implementing that create a new log file named by longdate every app starting up?

Thanks a lot.

question

Most helpful comment

It does work, thanks a lot

All 4 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

I have implementing it by inherit FileTarget class.

[Target("MyFirst")]
    public sealed class MyFirstTarget : FileTarget
    {
        public MyFirstTarget()
        {
            DateTime t1 = DateTime.Now;
            string s1 = t1.ToString("yyyy-MM-dd hh:mm:ss fff");
            FileName = "${basedir}/logs/info/" + s1 + ".txt";
        }
    } 

config file insert it

  <extensions>
    <add assembly="Ext1"/>
  </extensions>

<target name="first" type="MyFirst" encoding="utf-8" layout="${longdate} ${callsite} [${level}]: ${message}" />

i thank it can be implemeted by a esay way.

You could use cached:

${longdate:cached=true}

See https://github.com/NLog/NLog/wiki/Cached-Layout-Renderer

Related, the latest example in that page

It does work, thanks a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carkov1990 picture carkov1990  Â·  3Comments

JustArchi picture JustArchi  Â·  3Comments

Jerefeny picture Jerefeny  Â·  3Comments

ranjan-2209 picture ranjan-2209  Â·  3Comments

Sam13 picture Sam13  Â·  3Comments