一些關於怎樣把log4net資訊輸出到UI介面的思路
如果需要C#程式加入日誌功能,那log4net絕對是一個不錯的選擇。
- 經過一些簡單的配置,就能實現各種不同需求的日誌功能了
- 保持你的code儘量的簡潔了,也不影響單元測試
- 不需要考慮多執行緒
- ...
我用了之後,再也回不去那些沒有log4net的日子了。
在使用過程中,想把log4net的資訊同步顯示到UI某個控制元件中。以下是我的做法。
首先定義一個EventArgs
public class UiLogEventArgs : EventArgs { public string Message { get; private 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
相關文章
- 關於 API介面的一些知識分享API
- 使用Log4Net根據log level的不同將log輸出到不同的檔案中
- 關於思路
- WPF 怎麼把checkbox改成開關樣式
- [20190505]關於latch 一些統計資訊.txt
- LogMasker:避免將敏感資訊輸出到Log4j等日誌
- 關於element ui input標籤的改造樣式UI
- 關於SSL證書的一些介紹
- 將程式碼中的除錯資訊輸出到日誌檔案中除錯
- 關於 SAP UI5 Device API 的使用介紹UIdevAPI
- 關於 sap.ui.base.Object 的簡要介紹UIObject
- 關於SAP UI5資料繫結我的一些原創內容UI
- Log4Net配置詳解及輸出自定義訊息類示例
- NLPIR在文字資訊提取方面的優勢介紹
- 總結一些滲透測試中資訊收集思路
- logstach 8.6.2輸出到mongo 6Go
- C語言將資料表輸出到終端C語言
- 關於雲伺服器的一些問題介紹伺服器
- 關於資訊保安的
- Vue+element ui table 匯出到excelVueUIExcel
- WebRTC:資料傳輸相關協議簡介Web協議
- Xamarin.FormsShell基礎教程(7)Shell專案關於頁面的介紹ORM
- setuptools把python程式釋出到pypi的攻略Python
- 關於 SAP UI5 MessageProcessor 訊息建立的問題UI
- 基於ctfshow的資訊收集思路與CTF實戰
- 關於ChatGPT的一些資訊,我畫了一張思維導圖ChatGPT
- filebeat 收集nginx日誌輸出到kafkaNginxKafka
- 關於 SAP 電商雲 Spartacus UI Navigation Service 執行的一些明細UINavigation
- 關於印度跨境資料傳輸,印度放寬了跨境資料傳輸
- 【BASIS】關於Hana的一些資源設定
- 關於全域性引入element uiUI
- .net core 中使用Log4net輸出日誌到Mysql資料庫中MySql資料庫
- 關於抽象類和介面的初步理解抽象
- 關於模組裡面的註冊中心
- tee - 重定向輸出到多個檔案
- 怎麼修改elelment-ui的樣式UI
- 4.從輸入輸出到基本運算子的學習
- 關於 SAP UI5 OData V4 模型的 Property Binding 使用介紹UI模型
- 關於頁面無限滾動思路