2013-05-29 21 views
6

W mojej aplikacji internetowej próbuję utworzyć plik dziennika dla rejestrowania błędów i wyjątków, ale po uruchomieniu aplikacji plik dziennika nie jest tworzony w folderze rozwiązania lub w koszu teczka.Tworzenie ruchomego płaskiego pliku dziennika przy użyciu biblioteki Enterprise

Użyłem następującego kodu. Proszę, pomóż mi, że utknąłem z tym problemem, z góry dziękuję.

nazw stosowany

using Microsoft.Practices.EnterpriseLibrary.Logging; 

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 

.cs złożyć

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

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

Odpowiedz

0

Musisz ustaw kategorię w LogEntry, aby pasowała do źródła kategorii w pliku konfiguracyjnym ("Ogólne" w konfiguracji, którą wysłałeś). Sugerowałbym również przeglądnięcie bloku aplikacji do obsługi wyjątków, jeśli planujesz rejestrować wyjątki, zamiast pisać kod służący do samodzielnego tworzenia LogEntry.

4

Dla biblioteki korporacyjnej version 5.0 and above firma Microsoft wydała konfigurację biblioteki korporacyjnej tool, która umożliwia zmianę konfiguracji wizualnie w configuration file. Możesz również zainstalować to narzędzie z NuGet i dostęp do niego można uzyskać z wnętrza studio graficznego Tools >> Library Package Manager >> Manage NuGet Packages for solution i dodać odpowiednie EntLib Application Block. Możesz znaleźć przewodnik od here.

Korzystanie z tego narzędzia Mam wygenerowany plik konfiguracyjny, który przedstawia się następująco:

Place to w web.config lub app.config plik

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

Polecam że zamiast generowania plik dziennika w katalogu bin aplikacji należy określić fizyczną lokalizację, zmieniając wartość na coś podobnego do 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

Ogólnie rzecz biorąc, chcesz opublikować więcej niż zrzut kodu. Wyjaśnij, dlaczego to zadziała/etc. W ten sposób pomożesz zarówno OP, jak i przyszłym czytelnikom. – royhowie