事件记录器下的classlogfile写入错误

事件记录器下的classlogfile写入错误

本文介绍了事件记录器下的classlogfile写入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace utility
  /// <summary>
  /// ClassLog provides simple access to Application Event log
  /// </summary>
  public class ClassLog
  {

    private EventLog _Logger;
    private string _LogTarget;
    private string _LogEntry;

    /// <summary>
    /// CONSTRUCTOR
    /// </summary>
    /// <param name="Component_A">Component logger source string</param>
    public ClassLog(string Component_A)
    {
      //Logging is required by default
      if (!EventLog.SourceExists("Application"))
        throw (new Exception("Application event log does not exist."));

      // Create an EventLog instance and assign its source.
      _Logger = new EventLog();
      _Logger.Log = "Application";
      _Logger.Source = Component_A;
      _LogTarget = String.Empty;
      _LogEntry = String.Empty;
    }

    /// <summary>
    /// LogEntry will write an entry to the Application event log
    /// </summary>
    /// <param name="Target_A"></param>
    /// <param name="Entry_A"></param>
    /// <param name="EntryType_A"></param>
    public void LogEntry(
      string Target_A,
      string Entry_A,
      EventLogEntryType EntryType_A)
    {
      if (!EventLog.SourceExists("Application")) return;

      try
      {
        _Logger.WriteEntry(String.Format("Original Caller: {0} Source: {1}. Msg: {2}",
            _Logger.Source, Target_A, Entry_A), EntryType_A);
      }
      catch (Exception)
      {
      }
    }
    /// <summary>
    /// LogEntry for a generic information entry
    /// </summary>
    /// <param name="Target_A"></param>
    /// <param name="Entry_A"></param>
    public void LogEntry(
      string Target_A,
      string Entry_A)
    {
      LogEntry(Target_A, Entry_A, EventLogEntryType.Information);
    }

    /// <summary>
    /// LogException will write an exception to the Application event log
    /// </summary>
    /// <param name="ex"></param>
    public void LogException(ref Exception ex)
    {
      if (ex == null) return;
      if (!EventLog.SourceExists("Application")) return;

      try
      {
        // Write an error entry to the event log.
        _Logger.WriteEntry(String.Format("Original Caller: {0} Error: {1}. Source: {2}. Msg: {3}",
          _Logger.Source, ex.GetType().ToString(), ex.TargetSite, ex.Message), EventLogEntryType.Error);
      }
      catch (Exception)
      {
      }

    }

  }
}

推荐答案


这篇关于事件记录器下的classlogfile写入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:27