Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create new log file for each application run

As the title implies how can I create a new log file for each application run ? I know how to do it for minute/hour/etc. but not for app. run

There is what I have for now:

target name="Debug" archiveEvery="Hour"
archiveFileName="${basedir}/logs/Debug.{#####}.txt" maxArchiveFiles="4" 
archiveNumbering="Sequence" xsi:type="File" fileName="${basedir}/logs/Debug.txt" 
layout="${date:format=HH\:mm\:ss} | ${level} | ${message} ${exception}
${exception:format=stacktrace}"

But actually I dont need to archive every hour, what I want is to archive every time when I run my app. There is what I found in old forum, but I dont know how to use Cached_layout_renderer

like image 647
invis Avatar asked Jun 24 '13 06:06

invis


People also ask

Where to store log files Linux?

Linux has a special directory for storing logs called /var/log . This directory contains logs from the OS itself, services, and various applications running on the system.


2 Answers

I came across this problem myself, and it took me a long time to figure it out. Most of the answers I've found only have part of the solution and don't explain how/why it works... which makes it hard to adapt if you have a slightly different use case.

Example:

<target name="log"
   xsi:type="File"
   fileName="${basedir}/logs/log.${longdate:cached=true}.log"
   layout="${message}"
   archiveFileName="${basedir}/logs/archives/log.${shortdate}.{#}.log"
   archiveAboveSize="5242880"
   archiveEvery="Day"
   archiveNumbering = "Rolling"
   maxArchiveFiles="20" 
   />

Explanation

You have to use both the Cached Layout Renderer and the longdate variable. To understand why this works, you need to understand how they they work, and how they interact.

longdate:

fileName="${basedir}/logs/log.${longdate}.log"

Using the longdate variable in your log name will pretty much guarantee a new log file on every execution... except it creates a new log file every millisecond even during a single execution which is probably not desirable except in the most rare of circumstances.

Cached Layout Renderer:

fileName="${basedir}/logs/log.${shortdate:cached=true}.log"

Cached layout renderer will cache the variable on the first log call, and then always use that value for subsequent entries... but the cache only persists until the execution completes. Using shortdate, or any other variable that isn't guaranteed to changeon each execution, won't work. It will find a log file with the same filename it wants to use, and it'll just append (or delete if you have that set). This is not what we want.

Combined:

fileName="${basedir}/logs/log.${longdate:cached=true}.log"

This works because it takes the millisecond timestamp of the first log per execution, and then caches it, and always uses that logfile until the execution terminates (clearing the cache). Next time you run it (unless it's the same millisecond... unlikely!) you'll get a new value cached, and a new log file (but only one!).

like image 142
Mir Avatar answered Oct 21 '22 04:10

Mir


NLog's FileTarget support now a Property archiveOldFileOnStartup (NLog-3-2-0 Release).

archiveOldFileOnStartup="true"

There may be some issues with that, but if only a single instance of the App is running at the same time, it should have the desired behavior.

like image 29
Mikey Avatar answered Oct 21 '22 06:10

Mikey