• Why is the date appended twice on filenames when using Log4Net?


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

    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.log" />
        <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

    回答1:

    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.

    回答2:

    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. 

  • 相关阅读:
    安装MYSQL8.0提示api-ms-win-crt-runtime-l1-1-0.dll 丢失
    【.net】未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序解决办法
    Windows 2008 R2 配置 DNS 实现二级域名
    如何修改windows Server 2012 远程桌面连接默认端口
    System x 服务器制作ServerGuide U盘安装Windows Server 2012 R2操作系统
    MYSQL安装后自带用户的作用
    mysql 查看数据库、表的基本命令
    嵌入式必知基础算法
    嵌入式学习网站集合
    C避坑指南
  • 原文地址:https://www.cnblogs.com/chucklu/p/13224924.html
Copyright © 2020-2023  润新知