一些關於怎樣把log4net資訊輸出到UI介面的思路

zbbfb2001發表於2017-09-05

如果需要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

相關文章