为什么未将日志项写入事件查看器?

时间:2020-02-28 14:52:30

标签: c# windows logging

我有一个Windows服务,偶尔需要将其写入Windows事件日志。主要用于调试。我已经创建了一个安装程序,并在其中添加了日志条目。该日志是在事件查看器中创建的,但是我尝试写入的任何内容都没有出现。

项目安装程序非常简单:

namespace MyNamespace1
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
    }

[RunInstaller(true)]
public class MyEventLogInstaller : Installer
{
    private EventLogInstaller myEventLogInstaller;

    public MyEventLogInstaller()
    {
        // Create an instance of an EventLogInstaller.
        myEventLogInstaller = new EventLogInstaller();

        // Set the source name of the event log.
        myEventLogInstaller.Source = "RemBkAgent";

        // Set the event log that the source writes entries to.
        myEventLogInstaller.Log = "PP_RBAgent";

        // Add myEventLogInstaller to the Installer collection.
        Installers.Add(myEventLogInstaller);
    }

    public static void Main()
    {
        MyEventLogInstaller myInstaller = new MyEventLogInstaller();
    }
}
}

设计师:

namespace MyNamespace1
{
    partial class ProjectInstaller
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
        this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
        // 
        // serviceProcessInstaller1
        // 
        this.serviceProcessInstaller1.Password = null;
        this.serviceProcessInstaller1.Username = null;
        this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;  
        // 
        // serviceInstaller1
        // 
        this.serviceInstaller1.ServiceName = "Name Of TheService";
        this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Manual; 

        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceProcessInstaller1,
        this.serviceInstaller1});

    }

    #endregion

    private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
    private System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
}

使用installutil进行安装时,将在“应用程序和服务日志”下创建日志PP_RBAgent。到目前为止一切顺利。

我有一个全局事件日志对象:

private EventLog m_EventLog;

在OnStart中,我创建了m_EventLog并已验证它确实已创建(至少不为null):

protected override void OnStart(string[] args)
{
    LogSource = "RemBkAgent";
    LogName = "PP_RBAgent";
    m_EventLog = new EventLog(LogName, ".", LogSource);
}

最后,我有一个LogUpdateEvent函数,希望可以在其中写入日志

    private void LogUpdateEvent(double sngAction, string strActionText,
        bool blnFail, string strDetails = "", string strErrorMessage = "",
        bool blnWarning = false)
    {

            m_EventLog.WriteEntry(sngAction.ToString("#.00") + " " + strCompleteLogString, shrLogType, intEventID);
    }

但是什么也没写。没有引发任何错误。我这样做正确吗?

0 个答案:

没有答案
相关问题