事件的EventLog正在抛出一个错误,指出未设置源

时间:2016-06-22 21:56:36

标签: c# visual-studio service event-log

这是我第一次为应用程序设置服务,我几乎按照Microsoft website的步骤进行操作。为了按照网站的指示创建eventLog1,我去了设计视图并将事件日志拖放到服务中。从那里我跟着如何添加源和日志,但是当我运行应用程序时,我一直得到“在写入事件日志之前未设置源属性”。有没有更好的方法来创建这些?在事件日志之外,服务正确运行。

 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()

1 个答案:

答案 0 :(得分:0)

在构造函数 Workbooks(NewFileName).Activate Application.Run ("'" & NewFileName & "'!AUTORUN") 中,首先调用FGLFileTransferService(),然后才能设置自己的值。否则他们会被覆盖。

解决方案如下所示:

InitializeComponent()