2013-05-29 42 views
6

Trong ứng dụng web của tôi, tôi đang cố tạo tệp nhật ký để ghi lỗi và ngoại lệ, nhưng khi tôi chạy ứng dụng, tệp nhật ký không được tạo trong thư mục giải pháp của tôi hoặc trong thùng thư mục.Tạo tệp nhật ký phẳng bằng phẳng sử dụng thư viện doanh nghiệp

Tôi đã sử dụng mã sau. Xin hãy giúp tôi Tôi bị mắc kẹt với vấn đề này, cảm ơn bạn trước.

namespace sử dụng

using Microsoft.Practices.EnterpriseLibrary.Logging; 

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 

cs nộp

 public int GetdeskKMasterRecordsCount(string CircleId, string StoreId) 
     { 
      try 
      { 
       throw new Exception("this is normal exception"); 

      } 
      catch (Exception ex) 
      { 
       BindLog(ex); 
       return 0; 
      } 
     } 


     public static void BindLog(Exception ex) 
     { 
      if (ex == null) return; 
      Logger.Write(LogInformation(1, DateTime.Now, ex.Message, " ")); 

     } 

     public static LogEntry LogInformation(int eventId, DateTime timeStamp, string message, string documentName) 
     { 
      LogEntry logEntryObject = new LogEntry(); 
      logEntryObject.EventId = eventId; 
      logEntryObject.Title = documentName; 
      logEntryObject.TimeStamp = timeStamp; 
      logEntryObject.MachineName = System.Environment.MachineName; 
      logEntryObject.Message = message; 
      logEntryObject.Categories.Add("Exception"); 

      return logEntryObject; 
     } 

Web tập tin Config

<configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    <section name="recordingWindowGroup" type="Vodafone.DMS.BAL.WindowConfigurationHandler, Vodafone.DMS.BAL"/> 
    <section name="defaultParamGroup" type="Vodafone.DMS.BAL.DefaultParamConfiguration, Vodafone.DMS.BAL"/> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
    </configSections> 
    <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add name="Rolling Flat File Trace Listener" 
      type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
      fileName="Vodafone.DMS.log.exclude" footer="---------------------------" formatter="Text Formatter" header="---------------------------" rollFileExistsBehavior="Increment" 
      rollSizeKB="10" timeStampPattern="yyyy-MM-dd hh:mm:ss" maxArchivedFiles="7" traceOutputOptions="Timestamp, Callstack" filter="All"/> 
    </listeners> 
    <formatters> 
     <add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;" 
      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="General"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </allEvents> 
     <notProcessed switchValue="All" name="Unprocessed Category"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </notProcessed> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener"/> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration> 

Trả lời

0

Bạn cần phải đặt một danh mục trên LogEntry của bạn để phù hợp với một nguồn danh mục trong tệp cấu hình của bạn ('Chung' trong cấu hình bạn đã đăng). Tôi cũng khuyên bạn nên xem xét Khối Xử lý Ngoại lệ Ứng dụng nếu bạn dự định đăng nhập ngoại lệ, thay vì viết mã để xử lý việc tạo một LogEntry cho mình.

4

Đối với thư viện doanh nghiệp version 5.0 and above, Microsoft đã phát hành cấu hình thư viện doanh nghiệp tool cho phép bạn thay đổi cấu hình trực quan trong configuration file. Bạn cũng có thể cài đặt công cụ này từ NuGet và có thể truy cập từ bên trong studio trực quan Tools >> Library Package Manager >> Manage NuGet Packages for solution và thêm EntLib Application Block thích hợp. Bạn có thể tìm hướng dẫn từ here.

Sử dụng công cụ mà tôi đã tạo ra một tập tin cấu hình mà là như sau:

Nơi này trong Web.Config hoặc App.Config nộp

<loggingConfiguration name="loggingConfiguration" tracingEnabled="true" 
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     fileName="RollingFlatFile.log" 
     footer="---------------------------" formatter="Text Formatter" 
     header="---------------------------" rollFileExistsBehavior="Increment" 
     rollInterval="Week" rollSizeKB="20000" timeStampPattern="yyyy-MM-dd hh:mm:ss" 
     maxArchivedFiles="7" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" 
     filter="All" /> 
    </listeners> 
    <formatters> 
     <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;" 
     name="Text Formatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="General"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </add> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </allEvents> 
     <notProcessed switchValue="All" name="Unprocessed Category"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </notProcessed> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings"> 
     <listeners> 
      <add name="Rolling Flat File Trace Listener" /> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration> 

Tôi muốn giới thiệu rằng thay vì tạo ra các tệp nhật ký bên trong thư mục bin của ứng dụng, bạn nên chỉ định vị trí thực tế bằng cách thay đổi giá trị của fileName="RollingFlatFile.log" thành một cái gì đó như fileName="c:\logs\RollingFlatFile.log"

-2
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create(); 
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource); 
Logger.SetLogWriter(logWriterFactory.Create(), false); 
Logger.Write("btnStartProcess_Click"); 
Logger.Reset(); 
+0

Nói chung, bạn muốn đăng nhiều hơn kết xuất mã. Giải thích lý do tại sao điều này sẽ hoạt động/etc. Bạn sẽ giúp cả OP và các độc giả trong tương lai bằng cách làm như vậy. – royhowie

Các vấn đề liên quan