I'm trying to create a filter in FileAppender that will log just information filtered by 'ApplicationName'. I set the GlobalContext property in main Program and then I tried to use it in the filter block, but it doesn't works. Seems that the GlobalContext property is not being defined.
My Program.cs is liked this:
class Program
{
private static ILog log;
static void Main(string[] args)
{
string appName = "MyApp";
log4net.Config.XmlConfigurator.Configure();
log4net.GlobalContext.Properties["ApplicationName"] = appName;
log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Info Log!" + appName);
log.Error("Error Log!" + appName);
log.Warn("Warn Log!" + appName);
log.Debug("Debug Log!" + appName);
log.Fatal("Fatal Log test!" + appName);
Console.WriteLine("Hit enter");
Console.ReadLine();
}
}
And this is my appender in app.config:
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="app.log" />
<filter type="log4net.Filter.PropertyFilter">
<Key value="ApplicationName" />
<StringToMatch value="MyApp" />
</filter>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Date:%date Thread:[%thread] Level:%-5level Logger:%logger - ApplicationName:%P{ApplicationName}; Message:%message%newline" />
</layout>
</appender>
I also tried to instantiate ILog before setting the GlobalContext variable but I also get the same. Other thing that I tried was to override method FilterDecision to see what is coming in LoggingEvent, and there the Properties is empty.
I just need that this appender filter just by one specific Application.
Thanks!!