Why is the date appended twice on filenames when using Log4Net?
Asked Answered
G

6

23

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

Goingover answered 23/2, 2009 at 22:38 Comment(0)
G
8

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.

Goingover answered 27/2, 2009 at 1:57 Comment(1)
Full Control allows the user to change the permission set on a file, take ownership of the file, and perform actions permitted by all of the other file permissions.Busywork
S
11

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.

Skilful answered 25/3, 2009 at 4:26 Comment(0)
G
8

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.

Goingover answered 27/2, 2009 at 1:57 Comment(1)
Full Control allows the user to change the permission set on a file, take ownership of the file, and perform actions permitted by all of the other file permissions.Busywork
B
4

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.

Bridges answered 13/10, 2010 at 21:35 Comment(1)
That worked for me. It wasn't about permissions in my case.Mooncalf
L
1

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

Lassitude answered 23/2, 2009 at 23:7 Comment(6)
If I don't put the ".log", I will get filenames like this: "log.2009-02-23" with no extension or you can say, the date is the extension. I want to access the logs folder from the browser so I needed to add ".log" to the MIME type list of the folder in IIS.Goingover
Have you tried it without the ".lo\g" and just ".log" to see if the "\" is the problem?Lassitude
also, the "." at the beginning.Lassitude
I tried ".", nothing changed. I tried removing the "\" from "\g", like I said, it will be interpreted as something else. I got "log.2009-02-24.loA.D..2009-02-24.loA.D" as the file name. See that the "g" is now "A.D."Goingover
Well, I suppose it's my turn to apologize, and admit that no, I don't have the answer. Sorry =(Lassitude
No problem. Thanks for spending the time to check it out :) This might be a problem only in 64 bit so it's kinda weird.Goingover
B
1

I use the following:

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

With this I get filenames like: 2009.02.23.log

Busywork answered 26/2, 2009 at 9:26 Comment(1)
You don't actually need to escape the l and the o but I don't think it fixes my problem.Goingover
T
0

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

Tarriance answered 23/2, 2009 at 22:41 Comment(4)
datePattern tag accepts date and time formatted string as value. "g" is a valid date and time format pattern (google DateTimeFormatInfo) so i have to "escape" it so it would not get interpreted.Goingover
can you tell me more about the version of log4net you are using and the rest of your enviroment (os, .net version etc..). I have never had problems with having to escape the g from log.Tarriance
- Windows Server 2003 x64 Service Pack 2 - .NET Framework 2.0 - log4net 1.2.10 About the having to escape the g, look at my comment to devinb.Goingover
The datePattern value is processed through .Net's DateTime.ToString() method. It does not go through Log4Net's processing.Khalkha

© 2022 - 2024 — McMap. All rights reserved.