我使用EventSource通过EnterpriseLibrary.SemanticLogging将事件记录到SQL数据库,
以下是测试方法:
[TestMethod]
public void Log_Test()
{
var eventListener = new ObservableEventListener();
var sqlListener = SqlDatabaseLog.CreateListener("Instance", CloudConfigurationManager.GetSetting("ConnectionStringTrace"), "Traces");
eventListener.EnableEvents(ApplicationEventSource.Log, EventLevel.LogAlways, Keywords.All);
ApplicationEventSource.Log.ComponentNotLoaded("Milind", "Cheeta", "Core");
ApplicationEventSource类使用以下方法
从EventSource派生 [Event(2,
Keywords = Keywords.Component,
Message = "Component: {2} not successfully loaded for user: {0} on machine: {1}",
Task = Tasks.Load,
Opcode = EventOpcode.Start,
Level = EventLevel.Error)]
public void ComponentNotLoaded(string userName, string machineName, string componentName)
{
WriteEvent(2, userName, machineName, componentName);
}
WriteEvent时我没有收到错误,但没有记录到表跟踪
答案 0 :(得分:0)
请发布完整的ApplicationEventSource类。
如果你改变这样的方法:
public void ComponentNotLoaded(string userName, string machineName, string componentName)
{
if(IsEnabled())
WriteEvent(2, userName, machineName, componentName);
}
会调用WriteEvent吗?如果没有,结构中出现问题。尝试使用事件源分析器验证EventSource类。
请参阅https://msdn.microsoft.com/en-us/library/dn774985(v=pandp.20).aspx#_Checking_an_event
此外,设置数据库接收器的正常过程如下:
eventListener.LogToSqlDatabase(
Environment.MachineName,
sqlConnectionString,
);