如何为每个应用程序运行创建新的日志文件 [英] How to create new log file for each application run

查看:146
本文介绍了如何为每个应用程序运行创建新的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题意味着如何为每个应用程序运行创建一个新的日志文件?
我知道如何做它的分钟/小时/ etc。但不适用于app。



我现在有了:

  =DebugarchiveEvery =Hour
archiveFileName =$ {basedir} / logs / Debug。{#####}。txtmaxArchiveFiles =4
archiveNumbering = :type =FilefileName =$ {basedir} /logs/Debug.txt
layout =$ {date:format = HH \:mm \:ss} | $ {level} | $ {message} $ {exception}
$ {exception:format = stacktrace}

实际上我不需要归档每个小时,我想要的是存档每次当我运行我的应用程序。
我在旧论坛,但我不知道如何使用Cached_layout_renderer

解决方案

它花了我很长时间来弄清楚。
我发现的大多数答案只有一部分的解决方案,不解释如何/为什么它的工作...这使得它很难适应,如果你有一个稍微不同的用例。



  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
/>

说明



您必须同时使用缓存布局呈现器和longdate变量。



长期 要了解为什么会有效,您需要了解他们的工作原理, :

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

在日志名称中使用longdate变量将确保每次执行时都有一个新的日志文件...除了它创建一个新的



缓存布局渲染器

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

缓存布局渲染器将在第一次日志调用时缓存变量,然后始终使用该值对于后续条目...但缓存只持续到执行完成。使用shortdate或任何其他不能保证在每次执行中改变的变量将不会工作。它会找到一个具有相同文件名的日志文件,它只是追加(或删除,如果你有那个集合)。这不是我们想要的。



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

这是因为它需要每次执行的第一个日志的毫秒时间戳,然后缓存它,并始终使用该日志文件,直到执行终止(清除缓存)。下一次运行它(除非它是相同的毫秒...不太可能!)你会得到一个新的值缓存和一个新的日志文件(但只有一个!)。


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

解决方案

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!).

这篇关于如何为每个应用程序运行创建新的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆