使用 Log4Net 时,为什么在文件名上附加两次日期?

发布于 2024-07-13 21:40:33 字数 1160 浏览 6 评论 0 原文

我试图将日期添加到我的日志文件名中,并且通过遵循我在 stackoverflow 中找到的一些建议,我能够使其工作。 一切正常,但由于某种原因,第一个文件总是附加两次日期。

例如,我得到的不是 log.2009-02-23.log,而是 log.2009-02-23.log.2009-02-23.log

我发现它很奇怪,仅供参考,这是一个非常简单的代码。 这不像我让它在多线程环境中运行。

我的 log4net 配置:

<log4net>
<appender name="MyLog" type="log4net.Appender.RollingFileAppender">
    <file value="../../Logs/Mylog"/>
    <staticLogFileName value="false" />
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value=".yyyy-MM-dd.lo\g" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{DATE} [%t] %-5p %c - %m%n"/>
    </layout>
</appender>
<root>
    <level value="INFO"/>
    <appender-ref ref="MyLog"/>
</root>
</log4net>

有什么想法吗?

编辑:我想添加有关我正在测试的环境的信息。
- ASP.NET
- .net框架2.0
- Windows Server 2003 64 位服务包 2
- log4net 1.2.10

I was trying to add the date to my log file name and I was able to make it work by following the few suggestions I've found in stackoverflow. Everything works fine but for some reason, the first file always has the date appended twice.

For example, instead of log.2009-02-23.log, I get log.2009-02-23.log.2009-02-23.log.

I found it so weird and fyi, this is a very simple code. It's not like I have it running in a multi-threaded environment.

My log4net config:

<log4net>
<appender name="MyLog" type="log4net.Appender.RollingFileAppender">
    <file value="../../Logs/Mylog"/>
    <staticLogFileName value="false" />
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value=".yyyy-MM-dd.lo\g" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{DATE} [%t] %-5p %c - %m%n"/>
    </layout>
</appender>
<root>
    <level value="INFO"/>
    <appender-ref ref="MyLog"/>
</root>
</log4net>

Any ideas why?

Edit: I want to add the information about the environment I'm testing this in.
- asp.net
- .net framework 2.0
- windows server 2003 64-bit service pack 2
- log4net 1.2.10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撧情箌佬 2024-07-20 21:40:33

如果在初始化日志系统时访问日志文件时出现问题,就会发生这种情况。
如果您初始化日志系统两次,如果您在另一个副本正在运行并写入日志文件时运行程序,或者如果您正在文本编辑器中编辑日志文件,则可能会发生这种情况。 基本上,当 log4net init 运行时导致日志文件写锁定的任何情况。

检查代码中是否有对 log4net init 的重复调用 - 例如,您可能正在构造函数中初始化,而不是在单例的静态构造函数或全局 init 中进行初始化。

如果您在“网络花园”配置中运行并且文件名中不包含 PID,也可能会发生这种情况,因为每个不同的 Web 服务器进程都会尝试写入同一文件。 如果使用网络花园并写入文件,请将 pid 添加到文件名模式中,以便每个服务器进程获得自己的文件。

This happens if there is a problem accessing the log file when you initialize the log system.
It can happen if you initialize the log system twice, if you run your program while another copy is running and writing to the log file, or if you are editing the log file in a text editor. Basically anything that causes a write lock on the log file when log4net init runs.

Check your code for duplicate calls to log4net init - perhaps you are initializing in a constructor instead of in a singleton's static constructor or global init, for example.

This can also happen if you are running in a 'web garden' configuration and don't include the PID in the filename, because each different web server process tries to write to the same file. If using web gardens and writing to files, add the pid to the filename pattern so each server process gets its own file.

水染的天色ゝ 2024-07-20 21:40:33

这是一个权限问题。 至少这就是发生在我身上的事情。

我是 Log4Net 的新手,所以我不知道它有内部日志记录,但我发现了它,所以我尝试转动 内部登录
我不太确定它在说什么,但在我看来它正在做什么:
1. 将日期附加到文件名中。
2. 尝试访问该文件并对其进行写入(失败)。
3. 再次将日期附加到文件名中。
4. 成功访问文件(现在有奇怪的文件名)

在我知道这一点之前,我用谷歌搜索了这个问题的解决方案,就像我在这个 stackoverflow 问题上的标题一样。 那里没有那么多信息。 我发现也许有一个人说这种情况发生在某些人身上,但从未真正解释过原因或解决方案。 有了这些新信息(+来自 Log4Net 的内部错误消息),我正在查看来自搜索引擎的不同线程。 我发现这可能是权限问题的暗示。

写入应用程序似乎没有足够的权限访问日志文件夹。 应用程序的默认标识通常是NETWORK_SERVICE。 在我向该文件夹授予更多权限(我给了它完全控制权,但我不知道让它工作的最低限度是什么)后,它工作得很好。

如果有人能比我更好地解释这一点,请随时编辑。

It's a permission problem. At least that's what's happening to me.

I'm new in using Log4Net so I didn't know that it has internal logging but I found it so I tried turning internal logging on.
I wasn't very sure what it's saying but here's what it looks like to me it's doing:
1. Append the date to the file name.
2. Try to access the file to write to it (failed).
3. Append the date to the file name again.
4. Successfully access the file (which has the weird file name now)

Before I know this, I was google-ing for the solution to this problem with keywords like what I have as a title on this stackoverflow question. There wasn't that much information out there. I found maybe one guy who said it happens to some people but never really explained why nor the solution. With this new information (+the internal error message from Log4Net), I was looking at different threads from the search engines. With that I found hints that it might be a permission problem.

It seems that the writing application doesn't have sufficient permission to the logs folder. The default identity of the application is usually NETWORK_SERVICE. After I give more permission (I gave it full control but i don't know what is the minimum to make it work) to the folder, it works just fine.

If anyone can explain this better than me, please feel free to edit.

我恋#小黄人 2024-07-20 21:40:33

我遇到了同样的问题。 对我来说,它是使用 RollingFileAppender 来记录测试日志并使用 ReSharper 运行 NUnit 测试的组合。

事实证明,ReSharper 使用两个进程来运行测试:

2 TaskRunners

这会创建一个竞赛日志文件中的条件。

现在,如果我们更改日志文件名以包含进程 ID:

<appender name="MyLog" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="MyLog.pid.%processid" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="false"/>
  <datePattern value="_yyyy-MM-dd'.log'"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%15.15t] %-5p '%40.40c' - %m%n" />
  </layout>
</appender>

问题就解决了。 每个文件都有自己唯一的名称:

MyLog.pid.5440_2010-10-13.log
MyLog.pid.1496_2010-10-13.log

注意 PatternString 表示“类型”。

希望有帮助。

I run into the same problem. For me, it was a combination of using RollingFileAppender for my test logs, and running my NUnit tests with ReSharper.

It turns out that ReSharper uses two processes to run the tests:

2 TaskRunners

which creates a race condition on the log file.

Now, if we change the log file name to include the process id:

<appender name="MyLog" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="MyLog.pid.%processid" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="false"/>
  <datePattern value="_yyyy-MM-dd'.log'"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%15.15t] %-5p '%40.40c' - %m%n" />
  </layout>
</appender>

the problem is solved. Each file gets its own, unique name:

MyLog.pid.5440_2010-10-13.log
MyLog.pid.1496_2010-10-13.log

Note the use of PatternString for 'type'.

Hope that helps.

风柔一江水 2024-07-20 21:40:33

正如olle指出的那样。 您的问题与“\g”有关,您的 log4net 将其解释为另一种日期格式。
尝试删除“.yyyy-MM-dd.lo\g”并将其替换为“yyyy-MM-dd”

“.log”不属于日期格式

As olle pointed out. your problem is related to the '\g', which your log4net is interpreting as another dateformat.
Try deleting the ".yyyy-MM-dd.lo\g" and replacing it with "yyyy-MM-dd"

The ".log" doesn't belong in the dateformat

山色无中 2024-07-20 21:40:33

我使用以下内容:

<param name="DatePattern" value="yyyy.MM.dd.\l\o\g"/>

通过此我得到的文件名如下:2009.02.23.log

I use the following:

<param name="DatePattern" value="yyyy.MM.dd.\l\o\g"/>

With this I get filenames like: 2009.02.23.log

我乃一代侩神 2024-07-20 21:40:33

尝试 我不明白 \g 的用途。

try <datePattern value=".yyyy-MM-dd.lo\g" /> I don't understand what the \g is for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文