服务的事件日志抛出错误,指出未设置源

EventLog for a service is throwing an error stating source is not set

这是我第一次为应用程序设置服务,我几乎是按照 Microsoft website 上的步骤操作的。为了按照网站的指示创建 eventLog1,我转到设计视图并将事件日志拖放到服务中。从那里我遵循了如何添加源和日志,但是当我 运行 应用程序时,我不断收到 "Source property was not set before writing to the event log." 有没有更好的方法来创建这些?在事件日志之外,服务 运行 正确。

 public FGLFileTransferService()
    {
        eventLog1 = new System.Diagnostics.EventLog();
        if (!EventLog.SourceExists("FGLTransferServiceSource"))
        {
            EventLog.CreateEventSource("FGLTransferServiceSource", "FGLTransferServiceLog");
        }
        eventLog1.Source = "FGLTransferServiceSource";
        eventLog1.Log = "FGLTransferServiceLog";
        InitializeComponent();
    }

    public void OnDebug()
    {
        OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("Service Start");
        Timer timer = new System.Timers.Timer();
        //timer.Interval = 1; 
        timer.Elapsed += OnTimer;  //new System.Timers.ElapsedEventHandler(this.OnTimer);
        timer.Start();

    }

堆栈跟踪

   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message)
   at FGLFileTransfers.FGLFileTransferService.OnStart(String[] args) in C:\Users\brandon_paxton\Documents\Visual Studio 2015\Projects\FGLFileTransfers1.2\FGLFileTransfers\FGLFileTransferService.cs:line 42
   at FGLFileTransfers.FGLFileTransferService.OnDebug() in C:\Users\brandon_paxton\Documents\Visual Studio 2015\Projects\FGLFileTransfers1.2\FGLFileTransfers\FGLFileTransferService.cs:line 37
   at FGLFileTransfers.Program.Main(String[] args) in C:\Users\brandon_paxton\Documents\Visual Studio 2015\Projects\FGLFileTransfers1.2\FGLFileTransfers\Program.cs:line 26
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

在构造函数FGLFileTransferService()中先调用InitializeComponent(),然后才可以设置自己的值。否则它们将被覆盖。

解决方案如下所示:

public FGLFileTransferService()
    {
        // *** replaced this
        InitializeComponent();
        // *** replaced this
        eventLog1 = new System.Diagnostics.EventLog();
        if (!EventLog.SourceExists("FGLTransferServiceSource"))
        {
            EventLog.CreateEventSource("FGLTransferServiceSource", "FGLTransferServiceLog");
        }
        eventLog1.Source = "FGLTransferServiceSource";
        eventLog1.Log = "FGLTransferServiceLog";
            }