答案 0 :(得分:0)
关于配置.Asp.NET5 / MVC6有另一种配置方式,它可以读取任何格式的配置,最常见的是json,ini和Xml。
如果您在web.config中有配置,您仍然可以阅读它们并逐个浏览它们。
<configuration>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="Trace">
<listeners>
<add name="General Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="General.log" formatter="One Liner Formatter" header="" rollInterval="Day" traceOutputOptions="Callstack" asynchronous="false" asynchronousBufferSize="3000" />
<add name="Exception Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="Exceptions.log" formatter="Text Formatter" rollInterval="Hour" rollSizeKB="100" traceOutputOptions="Callstack" filter="Error" asynchronous="false" asynchronousBufferSize="3000" />
<add name="Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="Trace.log" footer="" formatter="One Liner Formatter" header="" rollInterval="Day" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId" asynchronousBufferSize="3000" />
<add name="SecurityAudit Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="SecurityAudit.log" footer="" formatter="One Liner Formatter" header="" rollInterval="Day" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId" asynchronousBufferSize="3000" />
<add name="Unprocessed Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="Unprocessed.log" footer="" formatter="One Liner Formatter" header="" rollInterval="Day" rollSizeKB="0" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" asynchronous="false" asynchronousBufferSize="3000" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}{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" />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)} Message: {message} Category: {category} Win32 ThreadId:{win32ThreadId} Extended Properties: {dictionary({key} - {value}|)}" name="One Liner Formatter" />
</formatters>
您可以像
一样阅读 var config = new Configuration()
.AddXmlFile(@"conf.xml");
var formatterName = config["loggingConfiguration::listeners:add:General Trace Listener:formatter"];
var logFileName = config["loggingConfiguration::listeners:add:General Trace Listener:fileName"];
var rollInterval = config["loggingConfiguration::listeners:add:General Trace Listener:rollInterval"];
然后您可以使用这些变量来构建LoggingConfiguration对象并将其传递给记录器..
像
using (LogWriter defaultWriter = new LogWriter(config))
{
var log = new LogEntry { Message = message };
defaultWriter.Write(log);
}
这似乎是一项繁琐的工作,如果您更喜欢使用Xml解析器,那么请继续,但最好的方法是在EL中查找解析器,因为它是开源的,并且它以与您相同的方式读取配置。