2
votes

Situation is that level of log4net is configured as 'Error' but there is some information i need to write under a 'no matter what' condition for example

"loggin has started" if only 'Error' or 'Fatal' is enabled i cant log this in Error or Fatal since its just information

so is there any way i can do that other than change the level of the logger to info, write the log and then change back the level because that will act just like a workaround not a solution

and without using Headers since they only come at the beginning

EDIT: In an Appender

StringMatchFilter stringFilter = new StringMatchFilter();
stringFilter.AcceptOnMatch = true;
stringFilter.StringToMatch = "successfully";
stringFilter.ActivateOptions();
appender.AddFilter(stringFilter);

DenyAllFilter deny = new DenyAllFilter();
deny.ActivateOptions();
appender.AddFilter(deny);

adding to an appender and setting level 'All' to the root and managing levels in appenders but still i am unable to write any message containing 'successfully' but please note when i set appender level to info the filter begins to work

2
Is there a reason why you aren't using the config file for log4net?IAmTimCorey
n appenders for n number of logging files all created new with DateTime appended to file name for n number of a class initializingBasit Anwer

2 Answers

2
votes

I assume this is all because you don't like the idea of using .FatalFormat when it is not really an error.

I'm not sure how you would do this programatically, but if you were using a config file you add a section like

    <logger name="ALWAYS">
        <level value="DEBUG" />
        appender-ref ref="RollingFileAppender" />
    </logger>

which means you can log your messages like

   log4net.LogManager.GetLogger("Always").InfoFormat( ... );

or you could create a static

   static readonly log4net.ILog alwaysLogger  = log4net.LogManager.GetLogger("Always");

and log via.

   alwaysLogger.InfoFormat(....);
1
votes

My suggestion would be to create a filter that looks first at a string match and then at the level. That way you could have a key string that you pass in the message (say "AppInfo" or something that would be unique and not found in an error). Then, your filter would pick that up and log it even when you logged it at the INFO level but the filter would ignore all other messages that weren't ERROR or FATAL. I wrote an article on CodeProject that will show you how to do complex filtering like this:

http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

The one key here is that you would probably need to not specify the filtering in the root for this particular appender, since root supersedes the appender if I remember correctly.