Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

c# - Using log4net to write to different loggers

I am using log4net to do my logging. I would like it to write to a file and to the eventlog at the same time.

For some reason, I find the messages twice in my logfile.

This is my app.config-section :

<log4net>
    <root>
        <level value="INFO" />
        <appender-ref ref="LogFileAppender" />
        <appender-ref ref="EventLogAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
        <param name="File" value="c:empDIS-logfile.txt" />
        <param name="AppendToFile" value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10MB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%date %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
        <param name="Indigo.DataIntakeService" value="eventlog" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %-5level %logger - %message%newline" />
            </layout>
    </appender>
</log4net>

In my code I have tof following :

private static readonly ILog Log = log4net.LogManager.GetLogger("DataIntakeService");
private static readonly ILog LogEvents = log4net.LogManager.GetLogger("EventLogAppender");

static void Main(string[] args)
{
    log4net.Config.XmlConfigurator.Configure();
}

public static void LogInfo(string message)
{
    Log.Info(message);
    LogEvents.Info(message);
}

It writes to my log-file, as requested, but the messages should also go to my eventviewer and that doesn't happen. It writes those messages to the logfile as well.

Where did I go wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have configured Log4Net so that the root logger logs to both file and event log appenders. All loggers inherit this configuration, so both your loggers "DataIntakeService" and "EventLogAppender" log to these appenders.

If you don't see log messages in event viewer, it may be because your application doesn't have permission to create the Event source.

UPDATE

How can i configure it so the DataIntakeService logs to the file and the other one to the eventviewer ?

Here's a sample configuration:

<log4net>
  <root>
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
  </root>
  <logger name="EventLogAppender" additivity="False">
    <level value="INFO" />
    <appender-ref ref="EventLogAppender" />
  </logger>
  <appender>
  ... 

With this sample:

  • The root logger (and hence all child loggers unless explicitly configured otherwise) will log to LogFileAppender. Your DataIntakeService logger isn't explicitly configured, so inherits this configuration.

  • The EventLogAppender logger is explicitly configured to log to EventLogAppender, and is configured not to inherit settings from parent loggers (additivity="false"). Therefore it doesn't log to LogFileAppender. If you set additivity="true" it will inherit settings and log to both LogFileAppender and EventLogAppender.

Incidentally, naming a logger EventLogAppender is perhaps a bit confusing: EventLogLogger might be a better name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...