我一直在尝试使用企业库在事件查看器和数据库中记录异常,对于事件查看器我已经成功记录异常但我无法在数据库中记录异常。主要问题是我也没有收到任何错误 我已使用企业控制台设置配置文件并将所有字段保留为默认值 我的网络配置部分看起来像这样
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="SampleLog" machineName="." traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"
filter="All" />
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
databaseInstanceName="Sample ADO Connection String" writeLogStoredProcName="WriteLog"
addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" autoFlush="false" name="General">
<listeners>
<add name="Event Log Listener" />
<add name="Database Trace Listener" />
</listeners>
</add>
<add switchValue="All" name="EventLogSample">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
<add switchValue="All" name="DBExceptions">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Database Trace Listener" />
<add name="Event Log Listener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</notProcessed>
<errors switchValue="All" autoFlush="false" name="Logging Errors & Warnings">
<listeners>
<add name="Database Trace Listener" />
<add name="Event Log Listener" />
</listeners>
</errors>
</specialSources>
我在 C# 代码中将记录器设置为
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
Logger.SetLogWriter(new LogWriterFactory().Create());
然后将异常记录为
LogEntry logEntry = new LogEntry();
logEntry.EventId = 100;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
logEntry.Categories.Add("DBExceptions");
Logger.Write(logEntry);
而企业库中的默认脚本是
sqlcmd -S (localdb)\v11.0 -E -i CreateLoggingDatabase.sql
sqlcmd -S (localdb)\v11.0 -E -i CreateLoggingDatabaseObjects.sql -d Logging
这是运行此错误后的 cmd 文件被记录在事件查看器中但数据库中没有任何记录我尝试检查 (localdb)\v11.0 数据库,我认为这些默认脚本即 CreateLoggingDatabaseObjects.sql 和 CreateLoggingDatabase.sql没有被触发。请在这方面帮助我,我尝试了很多方法但没有任何效果,它在事件查看器中完美记录。
最佳答案
之后,静静的上网冲浪终于得到了解决方案 首先,您必须创建企业库安装的日志数据库 (localdb)\v11.0 的所有结构到您的数据库,即每个表和存储过程。 否则,您可以通过在 packages 文件夹中的 enterpriselibrary.logging.database 文件夹中执行 sql 脚本来实现它。 完成此操作后,您就可以在数据库中记录异常。 注意;您需要将这些脚本添加到要记录异常的数据库中
您还可以使用 Ent Lib 控制台配置您的监听器。 遗憾的是,对于 entlib 没有有用的博客,我会尽快继续并分享这些步骤。 希望对别人有帮助
关于c# - 无法使用企业库登录数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405401/