我有以下配置和代码,writer.logentry
没有抛出异常但是没有创建文件。
更新1:我更改了监听器,删除了事件日志并添加了平面文件日志。我仍然无法看到创建的.log文件
我想我在配置中遗漏了一些东西?
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="AskAndTrack.log" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, 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" name="General">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
catch(Exception ex)
{
var logEntry = new LogEntry();
logEntry.Message = ex.Message;
Logger.Write(logEntry);
}
答案 0 :(得分:3)
发布的配置看起来不错。如果记录发生错误,Enterprise Library将不会抛出异常 - 它将尝试将故障记录到错误特殊源(如果已配置)。您没有为错误添加跟踪侦听器,但我建议您这样做。
您的问题看起来可能是权限。您能否验证该进程是否有权创建名为AskAndTrack.log
的文件?或者可能提前创建文件或写入明确授予权限的文件夹(例如Everyone Full Control(仅供测试!))。
答案 1 :(得分:1)
在我看来,你错过了类别和优先级,这是我的片段。忽略辅助方法,它们只返回一个类别,例如“常规”和优先级,例如“错误”。
if (!Logger.Writer.IsLoggingEnabled()) return;
var logEntry = new LogEntry { Severity = GetTraceEventTypeFromPriority(severity) };
logEntry.Categories.Add(GetLogCategory(app_name, severity)); // CHANGED TO NONE BECAUSE SITECORE SUCKS
logEntry.Priority = (int)severity;
if (!Logger.ShouldLog(logEntry)) return;
logEntry.Message = message;
logEntry.Title = GetLogTitle(RequestDataManager.RequestData.blah, message, severity);
lock (_locker)
{
Logger.Write(logEntry);
}