2012-09-09 7 views
5

私が持っているように、configファイル:RollingFlatFileTraceListenerDataを追加する方法をプログラム

<configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
</configSections> 

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="Tracing" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" type="System.Diagnostics.ConsoleTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="System Diagnostics Trace Listener"/> 
    </listeners> 
    <formatters> 
     <add template="{message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter"/> 
    </formatters> 
    <categorySources> 
      <add switchValue="All" name="AppLog"> 
    <listeners> 
      <add name="System Diagnostics Trace Listener"/> 
    </listeners> 
    </add> 
</categorySources> 
<specialSources> 
    <allEvents switchValue="All" name="All Events"/>  
    <notProcessed switchValue="All" name="Unprocessed Category"/> 
    <errors switchValue="Off" name="Logging Errors &amp; Warnings"/> 
</specialSources> 
私が持っているコンソールリスナー以外にも

が、私はプログラム的RollingFlatFileTraceListenerDataを定義したい:

var listener = new RollingFlatFileTraceListenerData("AppLog", @"c:\log.log", "", "", 0, "yyyyMMdd-hhmm", Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollFileExistsBehavior.Increment, Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollInterval.Hour, TraceOptions.LogicalOperationStack, "Text Formatter"); 

方法新しく定義したリスナーをリスナーのリストにプログラムで追加することはできますか?

答えて

5

一般に、asp.netアプリケーションでは、プログラムでTraceListenersを追加する簡単な方法は、診断プログラムのTrace.Listeners.Add()メソッドを使用することです。私は)のApplication_Start(上の私のglobal.asax.csでこれをしたい:

using D = System.Diagnostics; 

... 

protected void Application_Start() 
{ 
    if (D.Trace.Listeners["MyTraceListener"] == null) 
    { 
     D.Trace.Listeners.Add(new MyTraceListener("") { Name = "MyTraceListener" }); 
    } 

    ... 

} 

私は、まれではあるが、複数回のApplication_Start()火を見てきたので、それが所定の位置にすでにだ場合、私がチェックするだけの理由があります。

+0

Application_Startは、アプリケーションプールの再起動がある場合にのみ複数回起動します。 IISは、一定期間要求がない場合や、リサイクルルールに基づいて自動的に処理します。しかし、それは新しい 'HttpApplication'で始まるので、問題ではありません。 –

関連する問題