2012-09-09 17 views
5

Mam plik konfiguracyjny jak:Jak dodać RollingFlatFileTraceListenerData programowo

<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> 

Poza słuchacza konsoli, które mam, chcę, aby zdefiniować RollingFlatFileTraceListenerData programowo:

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"); 

Jak czy programowo mogę dodać mój nowo zdefiniowany program nasłuchujący do listy programów nasłuchujących?

Odpowiedz

5

Ogólnie rzecz biorąc, w aplikacjach asp.net, prostym sposobem programowego dodawania obiektów TraceListeners jest diagnostyczna metoda Trace.Listeners.Add(). Lubię robić to w moich Global.asax.cs na Application_Start():

using D = System.Diagnostics; 

... 

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

    ... 

} 

jedynie sprawdzić, czy jest już na miejscu Powodem jest fakt, widziałem Application_Start() pożar więcej niż raz, aczkolwiek rzadko .

+0

Aplikacja_Start uruchamia więcej niż jeden raz tylko wtedy, gdy istnieje restart aplikacji. Usługi IIS robią to automatycznie, gdy nie było żądań przez określony czas lub w oparciu o zasady recyklingu. Nie powinno to jednak stanowić problemu, ponieważ rozpocznie się od nowego, nowego "HttpApplication" –

Powiązane problemy