Reputation: 55
This is the first time I have set up a service for an application, I pretty much followed the step-by-step on the Microsoft website. To create the eventLog1 as instructed by the website I went to design view and drag and dropped an event log into the service. From there I followed how to add the source and log, but when I run the app i keep getting "Source property was not set before writing to the event log." Is there a better way to create these? Outside of the eventlog the service runs correctly.
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();
}
Stack trace
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()
Upvotes: 2
Views: 1314
Reputation: 2605
In the constructor FGLFileTransferService()
first call the InitializeComponent()
and only after you can set your own values. Otherwise they will get overwritten.
The solution will look like this:
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";
}
Upvotes: 1