Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4net output to My Documents

Tags:

c#

log4net

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

<appender name="LogFile" type="log4net.Appender.RollingFileAppender">
  <appendToFile value="true"/>
  <file value="%USERPROFILE%\My Documents\MyApp\log.txt"/>
  ...snip...
</appender>

This works, until we run in on a PC which has a non-English Windows. Because then, SpecialFolder.MyDocuments points to the folder Mijn Documenten, while the log still goes to My Documents. Confusion ensues, because now our files are in two places.

I want to write my log to the "real" My Documents folder. How do I do this?

  • I tried to find an environment variable like %USERPROFILE%, but there doesn't seem to exist one for My Documents.
  • There's a registry key that defines the true location of My Documents but it's not accessible from application.conf.
  • I tried to override the File parameter of my appender programmatically, like this:

    public static void ConfigureLogger()
    {
        XmlConfigurator.Configure();
    
        Hierarchy hierarchy = (Hierarchy)log4net.LogManager.GetRepository();
        foreach (var appender in hierarchy.Root.Appenders)
        {
            if (appender is FileAppender)
            {
                var fileAppender = appender as FileAppender;
                var logDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyApp");
                var pathElements = fileAppender.File.Split(Path.DirectorySeparatorChar);
                var logFileName = pathElements.Last();
                var fullLogFilePath = Path.Combine(logDirectory, logFileName);
                fileAppender.File = fullLogFilePath;
            }
        }
    }
    

    This doesn't work either: when I inspect the internals of my logger, the File property happily reports Mijn Documenten, but in the mean time the logs still go to My Documents.

I'm running out of ideas!

like image 520
jqno Avatar asked Aug 21 '13 18:08

jqno


People also ask

Where do log4net logs go?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

Does log4net create directory?

When using a file appender, the destination folder does not have to exist. Log4net creates the folder. Using an administrator account, connect to the Coveo Master server.

What is rolling file Appender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.


1 Answers

Change the line

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

to

<file type="log4net.Util.PatternString" value="%envFolderPath{MyDocuments}\MyApp\log.txt" />
like image 63
sgmoore Avatar answered Oct 10 '22 04:10

sgmoore