Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#窗口服务能否自动登录到自定义事件源/日志_C#_Windows Services_Event Log_System.diagnostics - Fatal编程技术网

C#窗口服务能否自动登录到自定义事件源/日志

C#窗口服务能否自动登录到自定义事件源/日志,c#,windows-services,event-log,system.diagnostics,C#,Windows Services,Event Log,System.diagnostics,我有一个C#窗口服务,我在ProjectInstaller.Designer.cs文件中创建自定义事件源和日志名 private void InitializeComponent() { this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.

我有一个C#窗口服务,我在
ProjectInstaller.Designer.cs
文件中创建自定义事件源和日志名

private void InitializeComponent()
    {
        this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
        this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
        // 
        // serviceProcessInstaller1
        // 
        this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        this.serviceProcessInstaller1.Password = null;
        this.serviceProcessInstaller1.Username = null;
        // 
        // serviceInstaller1
        // 
        this.serviceInstaller1.ServiceName = "DemoService";
        this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

        var eventLog1 = new System.Diagnostics.EventLog();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";

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

    }
当我安装Windows服务时,日志名会出现在应用程序和服务日志下

现在,服务中所有未经处理的异常是否都可能以源代码“MySource”记录在
MyNewLog

protected override void OnStart(string[] args)
    {
        throw new NotImplementedException();
    }

目前,它的日志位于
应用程序
下,源代码为'Service1`

那么,在类构造函数中配置事件日志:

void InitiateEventLog(EventLog eventLog)
{
    if (!EventLog.SourceExists("MySource"))
    {
       EventLog.CreateEventSource("MySource", "MyNewLog");
    }
    eventLog.Source = "MySource";
    eventLog.Log = "MyNewLog";
}
您的构造函数应该如下所示:

public Service1()
{
    InitiateEventLog(EventLog);
    InitializeComponent();
}
然后,您可以在
OnStart
方法中捕获未处理的异常,然后编写自定义日志:

    protected override void OnStart(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        EventLog.WriteEntry($"Unhundeled Exception occurred: {(e.ExceptionObject as Exception).Message}");
    }

您是否正在捕获未处理的异常并使用日志类进行日志记录?在
应用程序下如何毫无异常地处理它的写入操作,在
MyNewLog
下如何写入?谢谢,但是eventLog1引用将如何在这里出现?当前数据库中不存在名称“eventLog1”context@user584018服务已包含EventLog属性。看看那房子。我更新了PostThank,但仍然没有在
MyNewLog
下写入任何内容。在
应用程序
@user584018下编写一个事件
服务声明
,我添加了一些更改,我希望它能起作用,谢谢,但它不会在
应用程序和服务
下创建任何自定义日志标记,异常事件仍然会转到
应用程序