gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:18:45 25 4
gpt4 key购买 nike

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

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

关于c# - 服务的事件日志抛出错误,指出未设置源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37978942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com