In wpf using log4net, how can I append exception and event info to one appender and append data to another appender

£可爱£侵袭症+ 提交于 2019-12-12 02:07:46

问题


I am building a wpf app to provide a UI to an embedded system. The UI sends commands and queries to the embedded system. The UI also periodically reads data from the embedded system. I am using log4net.

I want to append the data to one RollingLogFileAppender and append exceptions, state changes, events, etc to a different RollingLogFileAppender. My searching has found nothing yet. How can I split the logging like this?


回答1:


Just add two rolling file appenders, with different names. Set a different level filter on both. With the config below you can then do this;

log.Info("my data message"); // goes to data-log.txt
log.Warn("my warning"); // goes to error-log.txt
log.Error("error!"); // goes to error-log.txt

Config

<log4net>
    <appender name="DataAppender" type="log4net.Appender.RollingFileAppender">
        <file value="data-log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
            <param name="LevelMin" value="INFO"/>
            <param name="LevelMax" value="INFO"/>
        </filter>
    </appender>

    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
        <file value="error-log.txt" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
        </layout>
        <evaluator type="log4net.Core.LevelEvaluator">
            <threshold value="WARN"/>
        </evaluator>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="DataAppender" />
      <appender-ref ref="ErrorAppender" />
    </root>
</log4net>


来源:https://stackoverflow.com/questions/32173576/in-wpf-using-log4net-how-can-i-append-exception-and-event-info-to-one-appender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!