【IT168技术文档】
在本系列的技巧(1)和技巧(2)中分别介绍了使用外部配置文件,使用数据库记录配置信息两种方法,不知道大家有没有想过不使用任何配置文件,也不使用数据库而直接用编程的方法来实现呢?本文将会展示如何使用编程的方法来配置Logging Application Block。首先我们需要了解一下Logging Application Block中比较重要的几个对象:
1.LogFormatter
格式化对象,LogFormatter有TextFormatter和BinaryFormatter两种,多数情况下我们会使用TextFormatter,它通过一个Template来创建,一个完整的Template格式如下:
这里就不具体解释每一项的含义,大家可以参考有关文档,示例代码:Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline} Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline} Title:{title}{newline} Machine: {machine}{newline} Application Domain: {appDomain}{newline} Process Id: {processId}{newline} Process Name: {processName}{newline} Win32 Thread Id: {win32ThreadId}{newline} Thread Name: {threadName}{newline} Extended Properties: {dictionary({key} - {value})}{newline}
const string Template = "Timestamp: {timestamp}{newline}" + "Message: {message}{newline}" + "Category: {category}{newline}" + "Machine: {machine}{newline}"; TextFormatter formatter = new TextFormatter(Template);