How to add processid to log4net layout?












3















I'm gonna use log4net in my wpf application. And I need messages in my log looks like this:



11/8/2018 10:49:38 AM   13 (5368)       properties disabled.


where 13 is processId which writes this message. So it's pretty easy. But unfortunately I can't achieve this. So I just need an appropriate pattern layout for my log4net logger.



I've found the following message in faq section of log4net official site:




The following example sets the file name for a FileAppender to include
the current process id by specifying the %processid pattern in the
File property.




<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>


So and it works but only for file name not for layout in my log file. And I need to put this %processid into my layout. And my current layout is:



<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
</layout>


And my log just writes processid string to my log file.



22/11/2018 16:21:51,863 PM processid (1) - Exiting application.


I've also found a SO answer. And it works. But %processid property is initialized only once during startup. And in my app the writing proсes is often changes. So this solution is not suitable for me. And I guess it could be achieved by default log4net layout settings.



The other option is using type="log4net.Util.PatternString" as a type of my conversionPattern. But it is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern then %threadId, %level and even %message will be printed as string constants).



23/11/2018 16:22:52,456 PM 31560 [thread] level - message


But I need both %threadId and %processid in log.










share|improve this question

























  • @pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

    – isxaker
    Nov 26 '18 at 8:10













  • @pfx nice, thank you

    – isxaker
    Nov 26 '18 at 9:12











  • Can you make a small edit on your question? Without it, I can't remove the flag

    – pfx
    Nov 26 '18 at 9:13











  • @pfx I've just done some changes

    – isxaker
    Nov 26 '18 at 9:18






  • 1





    Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

    – pfx
    Nov 26 '18 at 9:22
















3















I'm gonna use log4net in my wpf application. And I need messages in my log looks like this:



11/8/2018 10:49:38 AM   13 (5368)       properties disabled.


where 13 is processId which writes this message. So it's pretty easy. But unfortunately I can't achieve this. So I just need an appropriate pattern layout for my log4net logger.



I've found the following message in faq section of log4net official site:




The following example sets the file name for a FileAppender to include
the current process id by specifying the %processid pattern in the
File property.




<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>


So and it works but only for file name not for layout in my log file. And I need to put this %processid into my layout. And my current layout is:



<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
</layout>


And my log just writes processid string to my log file.



22/11/2018 16:21:51,863 PM processid (1) - Exiting application.


I've also found a SO answer. And it works. But %processid property is initialized only once during startup. And in my app the writing proсes is often changes. So this solution is not suitable for me. And I guess it could be achieved by default log4net layout settings.



The other option is using type="log4net.Util.PatternString" as a type of my conversionPattern. But it is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern then %threadId, %level and even %message will be printed as string constants).



23/11/2018 16:22:52,456 PM 31560 [thread] level - message


But I need both %threadId and %processid in log.










share|improve this question

























  • @pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

    – isxaker
    Nov 26 '18 at 8:10













  • @pfx nice, thank you

    – isxaker
    Nov 26 '18 at 9:12











  • Can you make a small edit on your question? Without it, I can't remove the flag

    – pfx
    Nov 26 '18 at 9:13











  • @pfx I've just done some changes

    – isxaker
    Nov 26 '18 at 9:18






  • 1





    Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

    – pfx
    Nov 26 '18 at 9:22














3












3








3


1






I'm gonna use log4net in my wpf application. And I need messages in my log looks like this:



11/8/2018 10:49:38 AM   13 (5368)       properties disabled.


where 13 is processId which writes this message. So it's pretty easy. But unfortunately I can't achieve this. So I just need an appropriate pattern layout for my log4net logger.



I've found the following message in faq section of log4net official site:




The following example sets the file name for a FileAppender to include
the current process id by specifying the %processid pattern in the
File property.




<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>


So and it works but only for file name not for layout in my log file. And I need to put this %processid into my layout. And my current layout is:



<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
</layout>


And my log just writes processid string to my log file.



22/11/2018 16:21:51,863 PM processid (1) - Exiting application.


I've also found a SO answer. And it works. But %processid property is initialized only once during startup. And in my app the writing proсes is often changes. So this solution is not suitable for me. And I guess it could be achieved by default log4net layout settings.



The other option is using type="log4net.Util.PatternString" as a type of my conversionPattern. But it is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern then %threadId, %level and even %message will be printed as string constants).



23/11/2018 16:22:52,456 PM 31560 [thread] level - message


But I need both %threadId and %processid in log.










share|improve this question
















I'm gonna use log4net in my wpf application. And I need messages in my log looks like this:



11/8/2018 10:49:38 AM   13 (5368)       properties disabled.


where 13 is processId which writes this message. So it's pretty easy. But unfortunately I can't achieve this. So I just need an appropriate pattern layout for my log4net logger.



I've found the following message in faq section of log4net official site:




The following example sets the file name for a FileAppender to include
the current process id by specifying the %processid pattern in the
File property.




<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-[%processid].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>


So and it works but only for file name not for layout in my log file. And I need to put this %processid into my layout. And my current layout is:



<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
</layout>


And my log just writes processid string to my log file.



22/11/2018 16:21:51,863 PM processid (1) - Exiting application.


I've also found a SO answer. And it works. But %processid property is initialized only once during startup. And in my app the writing proсes is often changes. So this solution is not suitable for me. And I guess it could be achieved by default log4net layout settings.



The other option is using type="log4net.Util.PatternString" as a type of my conversionPattern. But it is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern then %threadId, %level and even %message will be printed as string constants).



23/11/2018 16:22:52,456 PM 31560 [thread] level - message


But I need both %threadId and %processid in log.







c# .net logging log4net log4net-configuration






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:18







isxaker

















asked Nov 23 '18 at 11:03









isxakerisxaker

3,39053967




3,39053967













  • @pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

    – isxaker
    Nov 26 '18 at 8:10













  • @pfx nice, thank you

    – isxaker
    Nov 26 '18 at 9:12











  • Can you make a small edit on your question? Without it, I can't remove the flag

    – pfx
    Nov 26 '18 at 9:13











  • @pfx I've just done some changes

    – isxaker
    Nov 26 '18 at 9:18






  • 1





    Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

    – pfx
    Nov 26 '18 at 9:22



















  • @pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

    – isxaker
    Nov 26 '18 at 8:10













  • @pfx nice, thank you

    – isxaker
    Nov 26 '18 at 9:12











  • Can you make a small edit on your question? Without it, I can't remove the flag

    – pfx
    Nov 26 '18 at 9:13











  • @pfx I've just done some changes

    – isxaker
    Nov 26 '18 at 9:18






  • 1





    Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

    – pfx
    Nov 26 '18 at 9:22

















@pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

– isxaker
Nov 26 '18 at 8:10







@pfx actually it's not a duplicate.My original question had a link to that SO answer as well as explanation why it's not suitable for me. I have a bit different purpose.%processid has to be printed in my log file in each line. And it should be printed with the other parameter(%threadId, %level etc).So type="log4net.Util.PatternString" is not suitable as well(if I use this type - type="log4net.Util.PatternString" - in conversionPattern %threadId, %level'will be printed as string constants). So I need both %threadId and %processid in log(%processid DOES change in runtime)

– isxaker
Nov 26 '18 at 8:10















@pfx nice, thank you

– isxaker
Nov 26 '18 at 9:12





@pfx nice, thank you

– isxaker
Nov 26 '18 at 9:12













Can you make a small edit on your question? Without it, I can't remove the flag

– pfx
Nov 26 '18 at 9:13





Can you make a small edit on your question? Without it, I can't remove the flag

– pfx
Nov 26 '18 at 9:13













@pfx I've just done some changes

– isxaker
Nov 26 '18 at 9:18





@pfx I've just done some changes

– isxaker
Nov 26 '18 at 9:18




1




1





Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

– pfx
Nov 26 '18 at 9:22





Can you try using ThreadContextinstead of GlobalContext log4net.ThreadContext.Properties["pid"] = Process.GetCurrentProcess().Id; which allows to have a different property value per thread.

– pfx
Nov 26 '18 at 9:22












1 Answer
1






active

oldest

votes


















5





+50









You can implement a custom PatternLayoutConverter which outputs the process Id.

Doing so, you don't have to have to set and track the Id of the running process.



namespace PFX
{
class ProcessIdPatternLayoutConverter : PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
Int32 processId = Process.GetCurrentProcess().Id;
writer.Write(processId);
}
}
}


You then reference this PatternLayoutConverterin your Log4netconfig via its fully qualified assembly name as below.



<layout type="log4net.Layout.PatternLayout">                        
<converter>
<name value="processid" />
<type value="PFX.ProcessIdPatternLayoutConverter, PFX.Lib" />
</converter>
<conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
</layout>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445478%2fhow-to-add-processid-to-log4net-layout%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5





    +50









    You can implement a custom PatternLayoutConverter which outputs the process Id.

    Doing so, you don't have to have to set and track the Id of the running process.



    namespace PFX
    {
    class ProcessIdPatternLayoutConverter : PatternLayoutConverter
    {
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
    Int32 processId = Process.GetCurrentProcess().Id;
    writer.Write(processId);
    }
    }
    }


    You then reference this PatternLayoutConverterin your Log4netconfig via its fully qualified assembly name as below.



    <layout type="log4net.Layout.PatternLayout">                        
    <converter>
    <name value="processid" />
    <type value="PFX.ProcessIdPatternLayoutConverter, PFX.Lib" />
    </converter>
    <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
    </layout>





    share|improve this answer




























      5





      +50









      You can implement a custom PatternLayoutConverter which outputs the process Id.

      Doing so, you don't have to have to set and track the Id of the running process.



      namespace PFX
      {
      class ProcessIdPatternLayoutConverter : PatternLayoutConverter
      {
      protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
      {
      Int32 processId = Process.GetCurrentProcess().Id;
      writer.Write(processId);
      }
      }
      }


      You then reference this PatternLayoutConverterin your Log4netconfig via its fully qualified assembly name as below.



      <layout type="log4net.Layout.PatternLayout">                        
      <converter>
      <name value="processid" />
      <type value="PFX.ProcessIdPatternLayoutConverter, PFX.Lib" />
      </converter>
      <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
      </layout>





      share|improve this answer


























        5





        +50







        5





        +50



        5




        +50





        You can implement a custom PatternLayoutConverter which outputs the process Id.

        Doing so, you don't have to have to set and track the Id of the running process.



        namespace PFX
        {
        class ProcessIdPatternLayoutConverter : PatternLayoutConverter
        {
        protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
        Int32 processId = Process.GetCurrentProcess().Id;
        writer.Write(processId);
        }
        }
        }


        You then reference this PatternLayoutConverterin your Log4netconfig via its fully qualified assembly name as below.



        <layout type="log4net.Layout.PatternLayout">                        
        <converter>
        <name value="processid" />
        <type value="PFX.ProcessIdPatternLayoutConverter, PFX.Lib" />
        </converter>
        <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
        </layout>





        share|improve this answer













        You can implement a custom PatternLayoutConverter which outputs the process Id.

        Doing so, you don't have to have to set and track the Id of the running process.



        namespace PFX
        {
        class ProcessIdPatternLayoutConverter : PatternLayoutConverter
        {
        protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
        Int32 processId = Process.GetCurrentProcess().Id;
        writer.Write(processId);
        }
        }
        }


        You then reference this PatternLayoutConverterin your Log4netconfig via its fully qualified assembly name as below.



        <layout type="log4net.Layout.PatternLayout">                        
        <converter>
        <name value="processid" />
        <type value="PFX.ProcessIdPatternLayoutConverter, PFX.Lib" />
        </converter>
        <conversionPattern value="%date{dd/MM/yyyy HH:mm:ss,fff tt} %processid (%thread) - %message%newline" />
        </layout>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 10:06









        pfxpfx

        5,525122035




        5,525122035
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445478%2fhow-to-add-processid-to-log4net-layout%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud