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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:58:59+00:00 2026-05-30T05:58:59+00:00

In log4net i can easily set process id to to log file name from

  • 0

In log4net i can easily set process id to to log file name from config easily

<appender name="LogFileAppender" 
type="log4net.Appender.RollingFileAppender,log4net">

<file type="log4net.Util.PatternString" value="Log[%processid]" />
  • Can i do the same for the log4cxx from config file?
  • If yes, how?
  • 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-30T05:59:01+00:00Added an answer on May 30, 2026 at 5:59 am

    According to the log4cxx documentation in order to currently do this
    you need to declare at least one MappedDiagnostic context.

    An untested partial snippet of doing so is shown below

       #include <sys/types.h>
       #include <log4cxx/mdc.h>
       #include <iostream>
       #include <sstream>
    
    
       int main (int argc, char **argv)
       {
       //at the start of your program
       pid_t pid = getpid(); 
       pid_t tid = gettid();
       std::string pidstring;
       std::string tidstring;
       std::stringstream buffer;   
       buffer << pid << std::endl;
       pidstring = buffer.str();
       buffer.str(std::string());
       buffer << tid << std::endl;
       tidstring = buffer.str();
       buffer.str(std::string());
       MDC::put( "pid", pidstring);
       MDC::put( "tid", tidstring);
       // do actual stuff here
    
      return 0;   
      }
    

    After further inspection of the log4cxx source, I realised that file doesn’t take a ConversionPattern but FileNamePattern does. I believe you can only use FileNamePattern when you have use a TimeBasedRollingPolicy or FixedWindowRollingPolicy.

    Now you can add the processid to the log
    by adding the following parameter in your appender tags in the XML configuration file.

    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
                  <param name="FileNamePattern" value="MyApplication-%d{yyyy-MM-dd}- %X{pid}.log"/>
                  <param name="activeFileName" value="MyApplication.log"/>
    </rollingPolicy>
    <param name="file" value="appxDailyLog.log"/>
    

    or you can include it in the pattern layout by specifying the following layout tags also inside your appender tags in the XML configuration file.

     <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%X{pid} %X{tid} %d{yyyy-MM-dd HH:mm:ss,SSS}"/>
     </layout>
    

    There is no simple way from the configuration file only to have each process append its own process to its own log like you are familiar with in log4net.

    There were several log4cxx mailing list threads that mention dynamic log renaming, but all of them involved numerous changes in the C++ code and they don’t do what you request.

    The method they used involves having <param name="file" value="${logfilename}"/> where $logfilename is an environment variable that gets set by

    std::string filename ="MyApp-";
    filename.append(pidstring);
    logger = Logger::getLogger("Nameoflogger");
    setenv("logfile.name", "MyApp.log", 1);
    

    calling something like the above snippet in the C++ code, each time you want the log name to change.

    Other methods would involve patches to log4cxx as it currently doesn’t have the functionality you need.

    References

    log4cxx Conversion Pattern Wiki

    Short introduction to Apache log4cxx

    log4cxx Tutorial

    MDC log4cxx Class Reference

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

Sidebar

Related Questions

How can I log to special folders (e.g. %APPDATA% ) using the app.config file?
I'm using log4net and I configured my XML file to log into database with
really simple question -> i can't seem to get any data from Log4Net in
Currently I have the function CreateLog() for creating a a log4net Log with name
I'm using log4net to log write log message to a rolling log file. Now
We are trying to use Log4Net to log from our IIS 6-deployed WCF Application.
How can i configure the Common.Logging->log4net which log4net built with a different .snk file?
I know I can log exceptions to the database using Log4Net, but I want
I am already using Log4net to log errors to a file, however because of
I have a client-side application that uses log4net's RollingFileAppender and that can be instantiated

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.