简体   繁体   English

不是将EventLog使用Designer生成的代码放置在Windows Service应用程序中就可以了吗?

[英]Not EventLog dispose in Windows Service app using the Designer generated code is fine?

While creating a project using Windows Service VS2013 project template i noticed that after add an EventLog component from the tools box the #region named "Component Designer generated code" is filled with the following code: 在使用Windows Service VS2013项目模板创建项目时,我注意到从工具框中添加EventLog组件后,名为“组件设计器生成的代码”的#region将填充以下代码:

    private void InitializeComponent()
    {
        this.eventLog1 = new System.Diagnostics.EventLog();
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
        // 
        // Service1
        // 
        this.ServiceName = "Service1";
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

    }

My question is: Should add the Designer generate a line like: 我的问题是:应该添加Designer生成类似于以下内容的行:

components.Add(this.eventLog1);

after the line 下线后

this.eventLog1 = new System.Diagnostics.EventLog();

? The fact that exist a method like: 存在的方法类似于:

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

and that System.Diagnostics.EventLog implement the IDisposable interface make me think that such line should be included in Designer the generated code. 并且System.Diagnostics.EventLog实现了IDisposable接口,使我认为这样的行应包含在Designer中,并生成代码。 I know that is not a big deal and that just in very rare cases (eg you play dirty with ServicesBase derived instances in the Main method) the lack of that line could impact the performance of the app, but still not have sense to me have a Disposal method implementation like that and not add a line like: components.Add(this.eventLog1); 我知道这没什么大不了的,只是在极少数情况下(例如,您在Main方法中使用ServicesBase派生的实例打脏了),缺少该行可能会影响应用程序的性能,但对我而言仍然没有意义像这样的Disposal方法实现,而不添加以下行: components.Add(this.eventLog1); . Any thoughts? 有什么想法吗?

This looks like a bug to me. 在我看来,这似乎是个错误。 The EventLog class forgets to implement the proper constructor, one that takes the container reference. EventLog类忘记实现适当的构造函数,该构造函数接受容器引用。 Components are not required to do so, basic examples are BackgroundWorker and OpenFileDialog, they don't have anything to dispose. 不需要这样做,基本示例是BackgroundWorker和OpenFileDialog,它们没有任何可处理的内容。

But yes, EventLog does. 但是可以,EventLog可以。 Not so sure anybody would have noticed this before, event logging is usually done for the life of the program and the EventLog instance is usually stored in a static variable. 不确定是否有人会注意到这一点,事件日志通常在程序的整个生命周期内进行,而EventLog实例通常存储在静态变量中。 It is quite easy to fix: 修复起来很容易:

using System;
using System.ComponentModel;
using System.Diagnostics;

public class MyEventLog : EventLog {
    public MyEventLog() { }
    public MyEventLog(IContainer container) : this() { container.Add(this); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在带有Windows窗体的Designer生成的代码中使用#if指令进行条件编译 - Using the #if directive in designer generated code with windows forms for conditional compilation 在Winforms生成的.Designer.cs文件中修改Dispose(bool)时,是否需要将Dispose移至主代码文件? - When modifying Dispose(bool) in a winforms generated .Designer.cs file is it necessary to move Dispose to the main code file? Windows 服务未在自动启动时写入 EventLog - Windows Service not writing to EventLog on autostart 我如何阅读Windows服务的事件日志 - How do i read the eventlog of windows service 使用 Windows 服务安装创建 EventLog 源时出错 - Error on Creating EventLog source with windows service installation Specflow设计器生成的代码正在使用旧的specFlow 1.9.0.77 - Specflow designer generated code is using old specFlow 1.9.0.77 Windows Phone 7中的设计器代码 - Designer code in Windows phone 7 .NET Core 3.1 Windows 服务的事件日志源名称 - EventLog Source name for .NET Core 3.1 Windows Service 如何从Windows服务中动态加载的dll使用NLog写入eventLog - How to write to eventLog with NLog from a dynamically loaded dll in a windows service 如何处理 windows 服务的 Eventlog 属性 - How does the Eventlog property of a windows service get disposed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM