事件记录器下的classlogfile写入错误 [英] classlogfile writting error under event logger

查看:64
本文介绍了事件记录器下的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)
      {
      }

    }

  }
}

推荐答案

是的,Pete是对的,您不应该使用该异常.此处有关日志记录的文章

如何创建Windows事件日志以及如何编写自定义消息 [记录的艺术 [
Yes, Pete is right, you shouldn''t consume the exception. Here articles about logging

How To Create a Windows Event Log and Write your Custom Message as well[^]
The Art of Logging[^]


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆