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
c# log4net log4net-configuration log4net-appender log4net-filter
add a comment |
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
c# log4net log4net-configuration log4net-appender log4net-filter
add a comment |
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
c# log4net log4net-configuration log4net-appender log4net-filter
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
c# log4net log4net-configuration log4net-appender log4net-filter
asked 2 days ago
Radek Strugalski
1269
1269
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered 2 days ago
Radek Strugalski
1269
1269
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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