Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:00:21+00:00 2026-05-12T16:00:21+00:00

I am totally new to Log4net. I have managed to get something going by

  • 0

I am totally new to Log4net. I have managed to get something going by adding a config file and simple logging. I have hardcoded the value to be "C:\temp\log.txt" but this is not good enough.

The logs must go to the special folders

path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

and this path changes depending whether you are using Windows Server 2008 or Windows XP or Vista etc…

How can I just change the location of the file in log4net programmatically?

This is what I have done:

<configSections>
<section name="log4net"
         type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>         
    <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="C:\temp\log.txt" />
        <param name="AppendToFile" value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
        </layout>
    </appender>
</log4net>

class Program
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(Program));

    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Warn("Log something");

        path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);


        // How can I change where I log stuff?
    }
}

Just need to figure out how I can change to log stuff to where I want to.

Any suggestions?
Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:00:22+00:00Added an answer on May 12, 2026 at 4:00 pm

    log4net can handle this for you. Any appender property of type string can be formatted, in this case, using the log4net.Util.PatternString option handler. PatternString even supports the SpecialFolder enum which enables the following elegant config:

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
        <file type="log4net.Util.PatternString" 
            value="%envFolderPath{CommonApplicationData}\\test.txt" />
        ...
    </appender>
    

    Here’s a unit test that proofs the pudding:

    [Test]
    public void Load()
    {
        XmlConfigurator.Configure();
        var fileAppender = LogManager.GetRepository()
            .GetAppenders().First(appender => appender is RollingFileAppender);
    
        var expectedFile = 
            Path.Combine(
                Environment.GetFolderPath(
                    Environment.SpecialFolder.CommonApplicationData),
                    "test.txt");
    
        Assert.That(fileAppender, 
            Is.Not.Null & Has.Property("File").EqualTo(expectedFile));
    }
    

    The following test verifies that log4net actually writes to disk (which basically makes this an “integration” test, not a unit test, but we’ll leave it at that for now):

    [Test]
    public void Log4net_WritesToDisk()
    {
        var expectedFile = 
            Path.Combine(
                Environment.GetFolderPath(
                    Environment.SpecialFolder.CommonApplicationData),
                    "test.txt");
    
        if (File.Exists(expectedFile))
            File.Delete(expectedFile);
    
        XmlConfigurator.Configure();
    
        var log = LogManager.GetLogger(typeof (ConfigTest));
        log.Info("Message from test");
    
        LogManager.Shutdown();
    
        Assert.That(File.ReadAllText(expectedFile), 
            Text.Contains("Message from test"));
    }
    

    NB: I strongly suggest using the compact property syntax demonstrated in the above sample. Removing all those “<property name=” makes your config that much more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm totally new to AWS. I managed to have an instance that runs PHPMyAdmin.
I'm totally new on this so I want to start with something simple. I
I am totally new to SQL . I have a simple select query similar
I am totally new to Netty and Java, but this afternoon I have managed
Totally new to programming so kindly excuse the silly question. I have this URL
Totally new to Capistrano. I have a local git repository that I want to
Totally new to Drupal so be gentle please! Let's say I have a number
Totally new to Linux, have an Arch Linux machine up. What is the command
I'm totally new to flash, and I'm trying to build a very simple app.
I'm totally new to Ruby but not to programming. All I did was going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.