Почему элементы журнала не записываются в программу просмотра событий? - PullRequest
0 голосов
/ 28 февраля 2020

У меня есть служба 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 и проверил, что он действительно создан (по крайней мере, он не равен нулю) :

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);
    }

Но ничего не пишется. Ошибка не выдается. Я правильно это делаю?

...