如何在aspnet5 rc1 / mvc6中使用企业库

时间:2016-01-06 10:52:03

标签: c# configuration asp.net-core enterprise-library asp.net-core-mvc

  1. 如何在Aspnet5 Web应用程序项目中使用Enterprise Library?应该使用哪种nuget包?
  2. 在早期的asp.net网站中,web.config用于指定configSections,dataConfiguration,defaultDatabase,cachingConfiguration。如何/在何处在Aspnet5中指定这些值

1 个答案:

答案 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}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;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中查找解析器,因为它是开源的,并且它以与您相同的方式读取配置。