log4net filter by threadcontext property (.NET Core)











up vote
0
down vote

favorite












I am trying to create mulitple appenders each logging only certain messages. Filtering should be performed based on the selected property set to the ThreadContext. What I did is as follows (log4net.config)



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<log4net>

<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyy-MM-dd.log"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
</layout>
<filter type="log4net.Filter.PropertyFilter">
<Key value="Category" />
<StringToMatch value="3" />
<acceptOnMatch value="true" />
</filter>
</appender>

<appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLog_Security_"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyy-MM-dd.log"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
</layout>
<filter type="log4net.Filter.PropertyFilter">
<key value="Category" />
<stringToMatch value="1" />
<acceptOnMatch value="true" />
</filter>
</appender>

<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
<appender-ref ref="SecurityFileAppender" />
</root>

</log4net>
</configuration>


In order to set the property value prior to logging a message I issue the following line of code:



    log4net.ThreadContext.Properties["Category"] = "3";
_log.Info("Normal info");
log4net.ThreadContext.Properties["Category"] = "1";
_log.Info("Security info");


I tried configuring log4net with and without 'acceptOnMatch'.
In each case seems like the filter by thread context isn't working. Both messages appear in both files.



How should I fix the configuration in order to make filter by thread context property working? Is it doable at all?



Thanks, Radek










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to create mulitple appenders each logging only certain messages. Filtering should be performed based on the selected property set to the ThreadContext. What I did is as follows (log4net.config)



    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    </configSections>
    <log4net>

    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="MyLog_"/>
    <appendToFile value="true"/>
    <rollingStyle value="Composite"/>
    <datePattern value="yyyy-MM-dd.log"/>
    <maximumFileSize value="100MB"/>
    <staticLogFileName value="false"/>
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
    </layout>
    <filter type="log4net.Filter.PropertyFilter">
    <Key value="Category" />
    <StringToMatch value="3" />
    <acceptOnMatch value="true" />
    </filter>
    </appender>

    <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="MyLog_Security_"/>
    <appendToFile value="true"/>
    <rollingStyle value="Composite"/>
    <datePattern value="yyyy-MM-dd.log"/>
    <maximumFileSize value="100MB"/>
    <staticLogFileName value="false"/>
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
    </layout>
    <filter type="log4net.Filter.PropertyFilter">
    <key value="Category" />
    <stringToMatch value="1" />
    <acceptOnMatch value="true" />
    </filter>
    </appender>

    <root>
    <level value="ALL" />
    <appender-ref ref="FileAppender" />
    <appender-ref ref="SecurityFileAppender" />
    </root>

    </log4net>
    </configuration>


    In order to set the property value prior to logging a message I issue the following line of code:



        log4net.ThreadContext.Properties["Category"] = "3";
    _log.Info("Normal info");
    log4net.ThreadContext.Properties["Category"] = "1";
    _log.Info("Security info");


    I tried configuring log4net with and without 'acceptOnMatch'.
    In each case seems like the filter by thread context isn't working. Both messages appear in both files.



    How should I fix the configuration in order to make filter by thread context property working? Is it doable at all?



    Thanks, Radek










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to create mulitple appenders each logging only certain messages. Filtering should be performed based on the selected property set to the ThreadContext. What I did is as follows (log4net.config)



      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      <configSections>
      <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
      </configSections>
      <log4net>

      <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyy-MM-dd.log"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
      </layout>
      <filter type="log4net.Filter.PropertyFilter">
      <Key value="Category" />
      <StringToMatch value="3" />
      <acceptOnMatch value="true" />
      </filter>
      </appender>

      <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_Security_"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyy-MM-dd.log"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
      </layout>
      <filter type="log4net.Filter.PropertyFilter">
      <key value="Category" />
      <stringToMatch value="1" />
      <acceptOnMatch value="true" />
      </filter>
      </appender>

      <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="SecurityFileAppender" />
      </root>

      </log4net>
      </configuration>


      In order to set the property value prior to logging a message I issue the following line of code:



          log4net.ThreadContext.Properties["Category"] = "3";
      _log.Info("Normal info");
      log4net.ThreadContext.Properties["Category"] = "1";
      _log.Info("Security info");


      I tried configuring log4net with and without 'acceptOnMatch'.
      In each case seems like the filter by thread context isn't working. Both messages appear in both files.



      How should I fix the configuration in order to make filter by thread context property working? Is it doable at all?



      Thanks, Radek










      share|improve this question













      I am trying to create mulitple appenders each logging only certain messages. Filtering should be performed based on the selected property set to the ThreadContext. What I did is as follows (log4net.config)



      <?xml version="1.0" encoding="utf-8"?>
      <configuration>
      <configSections>
      <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
      </configSections>
      <log4net>

      <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyy-MM-dd.log"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
      </layout>
      <filter type="log4net.Filter.PropertyFilter">
      <Key value="Category" />
      <StringToMatch value="3" />
      <acceptOnMatch value="true" />
      </filter>
      </appender>

      <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="MyLog_Security_"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyy-MM-dd.log"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{HH:mm:ss.fff} [%4thread] [%property{Category}] %-5level %logger - %message%newline"/>
      </layout>
      <filter type="log4net.Filter.PropertyFilter">
      <key value="Category" />
      <stringToMatch value="1" />
      <acceptOnMatch value="true" />
      </filter>
      </appender>

      <root>
      <level value="ALL" />
      <appender-ref ref="FileAppender" />
      <appender-ref ref="SecurityFileAppender" />
      </root>

      </log4net>
      </configuration>


      In order to set the property value prior to logging a message I issue the following line of code:



          log4net.ThreadContext.Properties["Category"] = "3";
      _log.Info("Normal info");
      log4net.ThreadContext.Properties["Category"] = "1";
      _log.Info("Security info");


      I tried configuring log4net with and without 'acceptOnMatch'.
      In each case seems like the filter by thread context isn't working. Both messages appear in both files.



      How should I fix the configuration in order to make filter by thread context property working? Is it doable at all?



      Thanks, Radek







      c# log4net log4net-configuration log4net-appender log4net-filter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Radek Strugalski

      1269




      1269
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          To fix it I needed to add second filter configuration beneath each filter already defined as follows:



          <filter type="log4net.Filter.DenyAllFilter" />


          This makes the filter above working. Strange but this is how it works.



          Example fixed appender looks like this:



          <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
          ...
          <file value="MyLog_Security_"/>
          <filter type="log4net.Filter.PropertyFilter">
          <key value="Category" />
          <stringToMatch value="1" />
          <acceptOnMatch value="true" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          </appender>





          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',
            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%2f53418140%2flog4net-filter-by-threadcontext-property-net-core%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








            up vote
            1
            down vote













            To fix it I needed to add second filter configuration beneath each filter already defined as follows:



            <filter type="log4net.Filter.DenyAllFilter" />


            This makes the filter above working. Strange but this is how it works.



            Example fixed appender looks like this:



            <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
            ...
            <file value="MyLog_Security_"/>
            <filter type="log4net.Filter.PropertyFilter">
            <key value="Category" />
            <stringToMatch value="1" />
            <acceptOnMatch value="true" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            </appender>





            share|improve this answer

























              up vote
              1
              down vote













              To fix it I needed to add second filter configuration beneath each filter already defined as follows:



              <filter type="log4net.Filter.DenyAllFilter" />


              This makes the filter above working. Strange but this is how it works.



              Example fixed appender looks like this:



              <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
              ...
              <file value="MyLog_Security_"/>
              <filter type="log4net.Filter.PropertyFilter">
              <key value="Category" />
              <stringToMatch value="1" />
              <acceptOnMatch value="true" />
              </filter>
              <filter type="log4net.Filter.DenyAllFilter" />
              </appender>





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                To fix it I needed to add second filter configuration beneath each filter already defined as follows:



                <filter type="log4net.Filter.DenyAllFilter" />


                This makes the filter above working. Strange but this is how it works.



                Example fixed appender looks like this:



                <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
                ...
                <file value="MyLog_Security_"/>
                <filter type="log4net.Filter.PropertyFilter">
                <key value="Category" />
                <stringToMatch value="1" />
                <acceptOnMatch value="true" />
                </filter>
                <filter type="log4net.Filter.DenyAllFilter" />
                </appender>





                share|improve this answer












                To fix it I needed to add second filter configuration beneath each filter already defined as follows:



                <filter type="log4net.Filter.DenyAllFilter" />


                This makes the filter above working. Strange but this is how it works.



                Example fixed appender looks like this:



                <appender name="SecurityFileAppender" type="log4net.Appender.RollingFileAppender">
                ...
                <file value="MyLog_Security_"/>
                <filter type="log4net.Filter.PropertyFilter">
                <key value="Category" />
                <stringToMatch value="1" />
                <acceptOnMatch value="true" />
                </filter>
                <filter type="log4net.Filter.DenyAllFilter" />
                </appender>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Radek Strugalski

                1269




                1269






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418140%2flog4net-filter-by-threadcontext-property-net-core%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







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo