2012-09-09 37 views
5

Tôi có một tập tin cấu hình như:Làm thế nào để thêm RollingFlatFileTraceListenerData lập trình

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

Bên cạnh giao diện điều khiển người nghe mà tôi có, tôi muốn xác định một RollingFlatFileTraceListenerData lập trình:

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

thế nào thế nào tôi có thể thêm người nghe mới được định nghĩa của tôi vào danh sách người nghe theo chương trình?

Trả lời

5

Nói chung, trong các ứng dụng asp.net, một cách đơn giản để lập trình thêm TraceListeners là với phương pháp chẩn đoán 'Trace.Listeners.Add(). Tôi muốn làm điều này trong global.asax.cs của tôi trên Application_Start():

using D = System.Diagnostics; 

... 

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

    ... 

} 

Lý do duy nhất tôi kiểm tra xem nó đã được tại chỗ là vì tôi đã nhìn thấy nhiều Application_Start() Cháy hơn một lần, mặc dù không thường xuyên .

+0

Application_Start chỉ kích hoạt nhiều lần khi khởi động lại ứng dụng. IIS thực hiện điều đó tự động khi không có yêu cầu trong một khoảng thời gian, hoặc dựa trên các quy tắc tái chế. Tuy nhiên, không nên là một vấn đề, vì nó sẽ bắt đầu bằng một 'HttpApplication' mới –