Log4net output to My Documents

后端 未结 3 554
执念已碎
执念已碎 2021-01-17 17:51

In our C# app, we write files to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). Our log4net logfile should go there too, so we\'ve defined

相关标签:
3条回答
  • 2021-01-17 18:19

    The accepted answer is out of date.

    You should now use:

    <file value="${UserProfile}\Documents\log-messages.log" />
    

    (This should work even on Windows 7, where "Documents" is referred to by the alias "My Documents".)

    ${UserProfile} will map to C:\Users[UserName], even if you don't see this variable explicitly defined in your environment variable list.

    0 讨论(0)
  • 2021-01-17 18:24

    There seem to be at least 2 approaches. The simplest is kind of a hack:

    1. Specify a custom environment variable to indicate the root path in you log4net config:

      <file value="%MYAPP_USER_ROOTFOLDER%\MyApp\log.txt"/>

    2. At startup, before initializing logging, set this environment variable value:

      Environment.SetEnvironmentVariable("MYAPP_USER_ROOTFOLDER", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

    The more complicated but recommended approach is explained here: http://marc.info/?l=log4net-user&m=110142086820117&w=2 and here http://ziqbalbh.com/articles/log4net-another-way-to-change-log-file-location-on-runtime/

    0 讨论(0)
  • 2021-01-17 18:26

    Change the line

    <file value="%USERPROFILE%\My Documents\MyApp\log.txt"/>
    

    to

    <file type="log4net.Util.PatternString" value="%envFolderPath{MyDocuments}\MyApp\log.txt" />
    
    0 讨论(0)
提交回复
热议问题