Add date to log file name in Logging Application Block

江枫思渺然 提交于 2019-12-02 02:49:41

问题


I'm using the Microsoft Logging Application Block (version 5 beta 2) and I'm trying to dynamically set the file name of a log file to the year month and day (and then write a new log file every day). However, at runtime, the logger ignores any dynamic environment variables such as %date%, %time%, or %cd%. I can add static environment variables to the file name (such as %username%), but not dynamic environment variables. Does anyone know how to get the rolling flat file trace listener to dynamically set the date in the log file name?

(this is what I was setting the File Name as: Log_%date%.log)


回答1:


@Robertc, I know this question has been marked but I think I have a trick that you can use for your next project : Use your own environment variable!

E.g. Before you make any call to the Logging Block

Environment.SetEnvironmentVariable("MYDATE", "15/07/2010"); // << change the hardcoded date to get from DateTime
Debug.WriteLine (Environment.ExpandEnvironmentVariables(@"c:\Log\Log_%MYDATE%.log"));

In your app.config, in the appropriate Rolling flat file trace listener

FileName=Log_%MYDATE%.log

Note: This trick will only work if you restart your application daily.




回答2:


I gave up on the Logging Application Block and switched to log4net. Log4net lets me set the name of the log to today's date with a web.config setting. I wrote an article on Code Project that shows how to get log4net working with the Policy Injection Application Block here



来源:https://stackoverflow.com/questions/2647657/add-date-to-log-file-name-in-logging-application-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!