Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Log4Net have something comparable to Log4J's Markers?

Tags:

.net

log4net

Does log4net have something comparable to log4j's Markers?

Markers are objects that are used to add easily filterable information to log messages.

I've looked through the log4net API, but the ILog interface doesn't have any methods that accept any extra tokens that could be interpreted as a Marker type of thing.

For example, I only want to log to a single logger:

ILog logger = LogManager.GetLogger(typeof(MyClass));
logger.Error("This is an error!");

However, sometimes I will want these errors to be placed in the EventLog.

Currently I created a second logger with an EventLogAppender referenced. It inherits all the appenders from the root logger too.

That means I need 2 logger references in the class that is going to be logging. 1 for logging to event log, and 1 for logs that I don't want to go to event log:

ILog logger = LogManager.GetLogger(typeof(MyClass)); //root logger
ILog eventLogger = LogManager.GetLogger("EventLogger"); //eventlog logger
logger.Info("Some message that doesn't need to go to the event log.");
eventLogger.Error("Some error that does need to go to the event log.");

With markers, I'd be able to do something like

logger.Info(EVENT_LOG_MARKER, "This message should go to the event log.");

Then a filter would pickup the marker, and activate the appropriate appenders.

I could append a magic string to the beginning of the message, and pick that out with a Filter, but that is hackish at best it would seem.

Short of using magic strings, is there another option in log4net for implementing a Marker type system?

like image 785
crush Avatar asked Dec 01 '25 14:12

crush


1 Answers

There are no markers in the ILog interface of log4net, but you can use property filters to route your events to the correct logger: just create a single logger in your code and send all events to this logger. In your root configuration set the two appenders you want

root
    event log appender
    file appender

and then in each appender filter out messages that either have or not the correct property value, e.g.

<!-- filter in the event log appender -->
<filter type="log4net.Filter.Property">
    <key value="ToEventLog" />
    <stringToMatch value="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

Albeit it is magic strings ported to properties instead of the message, and you need to wrap your logging in some code setting the property: using a IDisposable to wrap the setup and teardown of the property could be a good idea, something like:

log.Info("To wherever!!");
using (var SendToEventLogSwitch = new EventLogSwitch)
{
    log.Info("To EventLog!!");
}

You could also use an extension method on the ILog to add the marker and use the property mechanism behind the scenes.

public static void EventLog(this ILog log, string message)
{
    log4net.ThreadContext.Properties["ToEventLog"] = "true";
    log.Info(message);
    log4net.ThreadContext.Properties["ToEventLog"] = "false";
}
like image 95
samy Avatar answered Dec 04 '25 08:12

samy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!