tags:

views:

240

answers:

1

Hi all new to log4net. My boss is forcing me to use it and I have an openmind to it. However i m struggling to find a configuration version that allows me to log to the special folders. I have found some solutions but I cannot seem to make them work.

I can do it programmatically but defeats the point. I have seen a post of using %envFolderPath but apparently is not available in the latest version but only in their current code.

Can somebody post a console demo logging to a special folder using config file? I would really appreciate it.

My c# code that I want to throw away is this

public void ExampleLog

{

        XmlConfigurator.Configure();

        var fileName = GetFileName();
        var appender = new log4net.Appender.RollingFileAppender
        {
            Layout = new log4net.Layout.PatternLayout("%d - %m%n"),
            File = fileName,
            MaxSizeRollBackups = 10,
            MaximumFileSize = "100MB",
            AppendToFile = true,
            Threshold = Level.Debug
        };
        appender.ActivateOptions();
        BasicConfigurator.Configure(appender);
    }
    private static string GetFileName()
    {
        const string subPath = "MySubFolder";
        var path = String.Format(@"{0}\{1}", Environment.GetFolderPath  (Environment.SpecialFolder.CommonApplicationData), subPath);
        const string logName = "Log.txt";
        return Path.Combine(path, logName);
    }

Any suggestion how to achieve the same via config file?

thanks for your time!

+1  A: 

Pretty sure the syntax for this is available in the current release.

<file type="log4net.Util.PatternString" value="%env{APPDATA}\\MyApp\\Log.txt" />

If you need something more, you can look into option of subclassing the PatternString class, as described here: Log4Net can’t find %username property when I name the file in my appender

Bryan Batchelder
Sorry for late response. Thanks!
I don't think this is a good idea, because on Vista and Windows 7, %APPDATA% resolves to the user's *roaming* directory, which means the log files will be synced to the domain server and downloaded each time the user logs in. See the comment here:http://stackoverflow.com/questions/1572934/where-to-store-an-application-log-file-on-windows/1573015#1573015
GuyBehindtheGuy