Issues when used in Windows Service

Dec 9, 2012 at 1:01 AM
Edited Dec 10, 2012 at 4:41 PM

I have configured Essential.Diagnostics to be used in a Windows Service.

However, it brings down the service with this exception (from the Event log):

Application: QLine.Services.WebApi.SelfHosted.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.CallbackException
   at System.Runtime.Fx+AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ListenerAsyncResult.IOCompleted(System.Net.ListenerAsyncResult, UInt32, UInt32)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

The same program works fine when I run it as a console application. The configuration is as follows:


      <!-- This listener creates file that can be read by the Service Trace Viewer tool -->
      <add name="QCare.EssentialTraceListener" 
           type="Essential.Diagnostics.RollingXmlTraceListener, Essential.Diagnostics"
           initializeData="{ApplicationName}-{DateTime:yyyyMMdd}.svclog" />
      <source name="QCare.WebApiSource" switchName="QCare.WebApiSwitch" switchType="System.Diagnostics.SourceSwitch">
          <!--Remove the default trace listener for better performance.-->
          <remove name="Default" />
          <!--Note: Removing the default trace listener prevents the dialog box 
                    from being displayed for Debug.Fail or Debug.Assert commands that are 
                    executed in user mode.-->
          <add name="QCare.EssentialTraceListener" />
      <!-- You can set the level at which tracing is to occur -->
      <add name="QCare.WebApiSwitch" value="Warning, Error" />
      <!-- You can turn tracing off -->
      <!--add name="QLine.WebApiSwitch" value="Off" -->
        <!-- activate a listener for web tracing (see <trace writeToDiagnosticsTrace="true"/> above-->
        <add name="QCare.EssentialTraceListener" />


Initially I had used a path for initializeData, so I thought this was the problem. But as one can see, I removed the path and just used "{ApplicationName}-{DateTime:yyyyMMdd}.svclog", and the issue remains the same. Once I comment out the line at the end where the listener is activated, then the applications runs fine as a service.

I have tried several user accounts for the service, LocalSystem, Administrator, etc., but this did not change anything.

I have also tried the ColouredConsoleTraceListener and the InMemoryTraceListener with the same issues.

Changing to the standard System.Diagnostics.TextWriterTraceListener resolves the problem, so I assume there might be an issue with how the Essential.Diagnostics listeners work in a WIndows Service.

Not sure what I am doing wrong.

Dec 10, 2012 at 9:10 PM

Some more information:

- This is deployed on .NET 4.5 (crash happens on both, Windows 8 and Windows Server 2008 R2).

- The service is a self-hosted Web Api application (no HttpContext!), but it has the capability of HTTP tracing (part of WebApi).

- in the App.config file I have HTTP tracing redirected to the standard Trace listeners

    <trace writeToDiagnosticsTrace="true"/>
    <customErrors mode="Off"/>

- If I create the trace listener manually and add it to the Trace.Listeners collection, it works OK (see service OnStart):

    protected override void OnStart(string[] args) {
      var config = ConfigureSelfHost();
      var newServer = new HttpSelfHostServer(config);
      var oldServer = Interlocked.CompareExchange<HttpSelfHostServer>(ref server, newServer, null);
      if (oldServer == null) {
        var listener = new Essential.Diagnostics.RollingXmlTraceListener(@"QCarePortal-{DateTime:yyyyMMdd}.svclog");

It seems the only (but most important!) condition for the crash to occur is:

- Run app as a service

- Use the App.config file to configure the Essential.Diagnostics listener

Feb 13, 2013 at 1:15 PM
Thanks for the detailed information. If you have a sample app with the minimum code to reproduce the issue, maybe log an issue in the tracker and attach the test case.
Apr 17, 2013 at 11:18 AM
Sorry, I have been unable to reproduce this issue, so I must be missing something.

I attached sample code to the issue.

I was able to create a webapi self-hosted project, installed as a Windows service, and using the config above.

I can successfully hit the page and get log messages written to the svclog file (from the Essential.Diagnostics listener).

If you have a minimum sample of code that reproduces the issue, such as a test project, could you please add it to the issue so I can investigate further.