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 6732619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:40:19+00:00 2026-05-26T10:40:19+00:00

There are several posts on the internet for the same topic and I am

  • 0

There are several posts on the internet for the same topic and I am also able to write multiple log files from my Windows Forms App. But my requirement is slightly different.

My app has two modes of running, say like “BuySomething” mode & “SellSomeOtherThing” mode. And when it is in “BuySomething” mode I want to write to Log_BuySomething.txt and to Log_SellSomeOtherThing.txt otherwise (one mode will be selected for sure).

In the app.config file, I have the same structure as in the last post of a StackOverflow Question.
My problem is that, when the code XmlConfigurator.Configure(); is executed, it creates empty log files of the names mentioned in both LogFileAppenders in the app.config file. I thought the following code would solve it, but it didn’t:

if (mode == BuySomeThing)
{
    logger = LogManager.GetLogger("LogFileAppender1");
}
else
{
    LogManager.GetLogger("LogFileAppender2");
}
XmlConfigurator.Configure();

How can I make sure that only the appropriate log file is created for that instance of the App?

  • 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-26T10:40:20+00:00Added an answer on May 26, 2026 at 10:40 am

    If I understand you, your app doesn’t switch between modes.

    If so, then I would suspect that using your code will create both files, but only actually write to one.

    If getting rid of the zero-byte file is important you could try something like:

    log4net.GlobalContext.Properties["MyLogFileName"] = 
       (mode == BuySomeThing) ? "Log_BuySomething.txt" : "Log_SellSomeOtherThing.txt" ;
    
    XmlConfigurator.Configure();
    

    You also need to change your config file to only have one appender and tell it to use this property for the file name, e.g.

    <appender name="FileAppender" type="log4net.Appender.FileAppender">
       <file type="log4net.Util.PatternString" value="Logfiles\%property{MyLogFileName}" />
    
        <appendToFile value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
    </appender>
    

    This should end up creating just one file. (In this example it will be created in the folder logfiles).

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

Sidebar

Related Questions

I know there are several threads and posts regarding this issue in the internet
So, there have been several posts here about importing and saving data from an
Possible Duplicate: Objective C for Windows I know there are several posts about this
I know, there are several posts with nearly the same question, but all the
I know there are several other posts on this topic but they still leave
There are several web applications which allow you to send photos from your mobile
I know there are several posts with a similar title. Most link to a
There are several rank posts out there but I have yet to see one
I know there have been several posts about random word generation based on large
OpenFlow is used in a lot iPhone apps, and there are several good posts

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.