简体   繁体   English

C#log4net:带空格的DateTimeFormat

[英]C# log4net: DateTimeFormat with blanks

I am using the RollingFileAppender(log4net) to log events of 24/7-application in a file. 我正在使用RollingFileAppender(log4net)将24/7应用程序的事件记录在文件中。 The file name have to be as follows: 文件名必须如下:

"blabla-2012- 3- 6.txt" or "blabla-2012-13- 6" or "blabla-2012- 3-12.txt" “ blabla-2012- 3- 6.txt”或“ blabla-2012-13- 6”或“ blabla-2012- 3-12.txt”

The problem is that I dont know a DateTime format, that would use blanks instead of zero. 问题是我不知道DateTime格式,该格式将使用空白而不是零。

RollingFileAppender appender = new RollingFileAppender ();
                 appender.Name = String.Format ("{0} appender_", name);
                 appender.File = string.Format ("{0} {1} {2} {3}", logPath, @ "\", file, name, extension);
                 appender.AppendToFile = true;
                 appender.LockingModel = new FileAppender.MinimalLock();
                 appender.StaticLogFileName = false;
              ->> Appender.DatePattern = string.Format ("yyyy-M-d");
....

You can place literals in the format string: 您可以将文字放在格式字符串中:

Appender.DatePattern = "yyyy- M- d"; // don't need string.Format

Does that not work? 那行不通吗?

Edit: I understand now: You want MM, but instead of "05" you want " 5". 编辑:我现在了解:您想要MM,但您想要“ 5”,而不是“ 05”。 DateTime formatting does not have a way to facilitate this. DateTime格式没有实现此目的的方法。 You could create your own derivative RollingFileAppender that tries to handle this special case formatting. 您可以创建自己的派生RollingFileAppender,尝试处理这种特殊情况的格式。

Log4net uses the following to determine the file name in the rolling file appender: Log4net使用以下内容来确定滚动文件附加器中的文件名:

m_scheduledFilename = 
CombinePath(File, m_now.ToString(m_datePattern, System.Globalization.DateTimeFormatInfo.InvariantInfo));

where as you guess m_now is the current datetime. 如您所料,m_now是当前日期时间。 So you can use all DateTime available custom formats , and I think the format you need is not present out of the box. 因此,您可以使用所有DateTime 可用的自定义格式 ,并且我认为您需要的格式不是开箱即用的。 So if you absolutely need such a format you can write a custom appender by deriving it from RollingFileAppender and overriding the RollOverTime method. 因此,如果您绝对需要这种格式,则可以通过从RollingFileAppender派生自定义附加器并覆盖RollOverTime方法来编写自定义附加器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM