一些关于怎样把log4net信息输出到UI界面的思路

如果需要C#程序加入日志功能,那log4net绝对是一个不错的选择。

- 经过一些简单的配置,就能实现各种不同需求的日志功能了

- 保持你的code尽量的简洁了,也不影响单元测试

- 不需要考虑多线程

- ...

我用了之后,再也回不去那些没有log4net的日子了。


在使用过程中,想把log4net的信息同步显示到UI某个控件中。以下是我的做法。

首先定义一个EventArgs

public class UiLogEventArgs : EventArgs
    {
        public string Message { getprivate set; }
 
        public UiLogEventArgs(string message)
        {
            Message = message;
        }
    }
然后自定义一个Appender

public class UiLogAppender : AppenderSkeleton
    {
        public event EventHandler<UiLogEventArgs> UiLogReceived;
 
        protected override void Append(LoggingEvent loggingEvent)
        {
            var message = RenderLoggingEvent(loggingEvent);
            OnUiLogReceived(new UiLogEventArgs(message));
        }
 
        protected virtual void OnUiLogReceived(UiLogEventArgs e)
        {
            if (UiLogReceived != null)
                UiLogReceived(this, e);
        }
    }

我是在XML实现配置log4net的

    <appender name="uiLogAppender" type="UiLog.UiLogAppender,Automation">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{HH:mm:ss,fff} %-5level - %message" />
      </layout>
      <threshold value="INFO" />
    </appender>

接下里就是在code中把被待处理的事件注册到UiLogAppender的事件EventHandler中

(首先我们得找到uiLogAppender,然后才把处理UI的方法注册上去)

 
 
var hierarchy = LogManager.GetRepository() as Hierarchy;
var appenders = hierarchy.Root.Repository.GetAppenders();
 
foreach (var appender in appenders)
{
    var uiLogAppender = appender as UiLogAppender;
    if (uiLogAppender != null)
        uiLogAppender.UiLogReceived += ShowMessageOnUi;
}

以上就是我把log4net信息同步输出到UI的方法。如果大家还有什么更好的方法,请告知。一起学习学习!


P.S. 

如果想尽快入门log4net,推荐一部视频教程 Application Instrumentation using log4net, 讲得很好。

|   application-instrumentation-log4net.zip
|
+---01. Overview
|       01. What to expect from this course (and what it expects of you).wmv
|       02. Course Outline.mp4
|       03. log4net's Beginnings.wmv
|       04. Quickstart  Getting Log4net Working.wmv
|       05. Some Helpful Log4net Resources.wmv
|
+---02. Configuring Log4Net
|       01. Log4Net Architecture and Configuration.wmv
|       02. Using The Default Configuration.wmv
|       03. XML Configuration.wmv
|       04. More on XML Configuration  The XmlConfigurator Attribute.mp4
|       05. Configuring Log4Net from Code.wmv
|       06. Common Configuration Gotchas.wmv
|
+---03. Logger Objects
|       01. Isolating Logging Concerns with Logger Objects.wmv
|       02. Logging Messages with Severity.wmv
|       03. Simple Formatting Methods of Loggers.wmv
|       04. Throttling Logger Output.wmv
|       05. Conditional Logging Properties.wmv
|       06. Using Multiple Loggers and The Logger Hierarchy.wmv
|       07. Attaching Appenders to Specific Loggers.wmv
|       08. Summary.wmv
|
+---04. Appenders
|       01. Shared Appender Properties.wmv
|       02. Console Appenders.wmv
|       03. Debug and Trace Appenders.wmv
|       04. EventLog Appender.wmv
|       05. File Appender.wmv
|       06. Rolling File Appender.wmv
|       07. ADO.NET Appender.wmv
|       08. ASP.NET Trace Appender.wmv
|       09. Remoting Appender.wmv
|       10. Telnet Appender.wmv
|       11. UDP Appender.wmv
|       12. SMTP and SMTPPickupDir Appenders.wmv
|       13. Forwarding and Buffering Appenders.wmv
|
+---05. Layouts and Patterns
|       01. Introduction to Layouts.wmv
|       02. The Simple Layout.wmv
|       03. The XML Layout.wmv
|       04. The Pattern Layout - Format Specifiers.wmv
|       05. The Pattern Layout - Format Modifiers.wmv
|       06. Raw Laouyts.wmv
|       07. Summary.wmv
|
+---06. Log Event Context
|       01. Introduction to Log Event Context.wmv
|       02. Quick Demo  Working with Custom Log Event Properties.wmv
|       03. Property Contexts.wmv
|       04. Context Property Stacks.wmv
|       05. Calculated Log Properties.wmv
|       06. Summary.wmv
|
+---07. Filters
|       01. Introduction to Filters.wmv
|       02. Level Match  Filtering Messages on A Single Severity Level.wmv
|       03. Level Range  Filtering Messages on A Range of Severity Levels.wmv
|       04. Logger Match  Filtering Messages on The Name of The Logger.wmv
|       05. String Match  Filtering Messages on The Log Message Contents.wmv
|       06. Property Match  Filtering Messages on The Value of A Log Property.wmv
|       07. Demo  Chaining Filters.wmv
|       08. Summary.wmv
|
+---08. Effective Logging
|       01. The Three Logging Mantras.wmv
|       02. Logging Code is Still Code.wmv
|       03. Coping with Null and Empty Values.wmv
|       04. Common Exception Logging Tactics.wmv
|       05. Logging Uses Resources  Lossy Logging.wmv
|       06. Log Now  Managing Log4net as A Cross-Cutting Dependency.wmv
|       07. Summary.wmv
|
+---09. Advanced Logging Tactics
|       01. Advanced Tactics  Reducing Friction, Object Patterns, and AOP.mp4
|       02. The 12-year Gap in The Log4net API.wmv
|       03. Applying Generics to Logger Object Creation.wmv
|       04. Implemeting Deferred Logging.wmv
|       05. Implementing Logger On-Demand.wmv
|       06. Disposable Activities.wmv
|       07. Logging Decorators.wmv
|       08. Logging with PostSharp Aspects.wmv
|       09. Summary.wmv
|
\---10. Extending Log4Net
        01. The Five Ways to Extend Log4net.wmv
        02. Creating and Using Custom Layouts.wmv
        03. Creating and Using Custom Filters.wmv
        04. Creating and Using Custom Appenders.wmv
        05. Creating and Using Object Renderers.wmv
        06. Creating and Using Plugins.wmv
        07. Summary.wmv

猜你喜欢

转载自blog.csdn.net/zbbfb2001/article/details/77847194