How to Write to Database by EnterPriseLibrary2005 Logging Application Block(项目心得)
继续互联星空项目,因为用到log,并且是写到Database里面,不想自己实现.
到愿意看看那个EnterPriseLibrary2005,微软的东西,好像是一个论坛搞的(pattern && practice),不管用了在说
1.先把例子找出来看看,大家先看看界面把
2.再看看界面上(按钮Log event information)主要调用的类
LogEntry log = new LogEntry();
log.EventId = this.eventForm.EventId;
log.Message = this.eventForm.Message;
log.Category = this.eventForm.Category;
log.Priority = this.eventForm.Priority;
// Writes the log entry.
Logger.Write(log);
看了以后大家也许都注意到了:LogEntry,Logger,
而Logger是静态的,
3.直接在解决方案里面加一个项目(我比较赖,而且这样也会避免一些不必要的错误)
我加的的是控制台程序(主要是为了test,因为以前没用过)
4.EnterPriseLibrary提供了一个好东西EntLibConfig.exe,(你可以在这个地址找到他C:\Program Files\Microsoft Enterprise Library\bin)这是一个配置文件生成工具,这样大家就不用一个劲的写
.config了^_^,先来看看界面
5.加一个application
6.在applicaiton1上点右键,我是建立的Logging block那项,
你可以看到2项,不要管configuration,
看logging and Instrumention Application Block ---------->Distributor Settring------->Sinks(看看这个下面有什么) event log sink 和 flat file sink这是干什么用的呢,想来从字面上我们也猜出几分了,我们可以看看logging and Instrumention Application Block ---------->Distributor Settring------->Categories------->General--------->点击Event Log那项,看看右边有什么!!!
那个sink我们在这儿找到了
7.现在,我们在sinks里面新建一个 Database Sink 程序加了一个 DataAccess Application Block
a.StoreProcName里面写上自己定义的存储过程名
b.DataAccess Application Block --------------------->Connection strings --------------->sql connection stirng ---------->里面的参数依照自己的来(我就出现过错误这里)
c. 在Event Log Destination,右边的那个sink 改为你新建的Database Sink 名字
8. 存储过程可以参考他给出的sql(C:\Program Files\Microsoft Enterprise Library\src\Logging\Sinks\Database\Scripts\LoggingDatabase.sql)
9.需要注意的地方
我是把项目加入然后取调试的,如果你们配置有问题要调试的话,要加入Logging.Sinks.Database项目,不要调试直接加入.dll
Logging.Sinks.Database项目里面有一个DatabaseSink.cs
{
DatabaseSinkData databaseSinkData = loggingConfigurationView.GetSinkData(ConfigurationName) as DatabaseSinkData;
DatabaseProviderFactory factory = new DatabaseProviderFactory(loggingConfigurationView.ConfigurationContext);
Data.Database db = factory.CreateDatabase(databaseSinkData.DatabaseInstanceName);
DBCommandWrapper cmd = db.GetStoredProcCommandWrapper(databaseSinkData.StoredProcName);
cmd.AddInParameter("eventID", DbType.Int32, logEntry.EventId);
cmd.AddInParameter("category", DbType.String, logEntry.Category);
cmd.AddInParameter("priority", DbType.Int32, logEntry.Priority);
cmd.AddInParameter("severity", DbType.String, logEntry.Severity.ToString());
cmd.AddInParameter("title", DbType.String, logEntry.Title);
cmd.AddInParameter("timestamp", DbType.DateTime, logEntry.TimeStamp);
cmd.AddInParameter("machineName", DbType.String, logEntry.MachineName);
cmd.AddInParameter("AppDomainName", DbType.String, logEntry.AppDomainName);
cmd.AddInParameter("ProcessID", DbType.String, logEntry.ProcessId);
cmd.AddInParameter("ProcessName", DbType.String, logEntry.ProcessName);
cmd.AddInParameter("ThreadName", DbType.String, logEntry.ManagedThreadName);
cmd.AddInParameter("Win32ThreadId", DbType.String, logEntry.Win32ThreadId);
cmd.AddInParameter("message", DbType.String, logEntry.Message);
cmd.AddInParameter("formattedmessage", DbType.String, FormatEntry(logEntry));
db.ExecuteNonQuery(cmd);
}
调用执行存储过程时候的代码,如果你有需要可以改此代码