views:

40

answers:

1

I have all of my service logic encapsulated in class library. When I instantiate the class library in a command line app I receive my trace information.

When I instantiate the class in a Windows Service, I see that my Custom trace listener has created the logs directory and start a file, but it stays a 0 KB.

Both applications have this in the .config:

 <system.diagnostics>
<switches>
  <add name="PTraceSwitch" value="Verbose" />
</switches>
<trace autoflush="true">
  <listeners>
    <add name="CustomXmlWriterTraceListener" />
    <add name="MyServiceEventListener"  />
    <remove name="Default" />
  </listeners>
</trace>
<sharedListeners>
  <add
     type="CustomUtilities.CustomXmlWriterTraceListener, CustomUtilities"
     name="CustomXmlWriterTraceListener"
     initializeData="Logs\MyService.svclog"
     RollLogAtMidnight="True"
     DateFormat="yyyy.MM.dd"
     MaxFileSizeMB="1"
     CompressLogsOlderThanTimeSpan="1.00:0:00"
     DeleteLogsOlderThanTimeSpan="30.00:00:00"/>

  <add name="MyServiceEventListener"
       type="System.Diagnostics.EventLogTraceListener"
       initializeData="MyServiceEventLog">
    <filter type="System.Diagnostics.EventTypeFilter"
      initializeData="Warning" />
  </add>
</sharedListeners>

A: 

It seems that my CustomXmlWriterTraceListener behaves differently when run with a service than a class library or exe.

I added this to my service to debug the main entry point and walk through the init of my listener:

Debugger.Launch();

The underlying Writer is null when I run my service, it is normally populated during the base constructor. I used .NET Reflector to see what the XmlWriterTraceListener does. There is an internal method EnsureWriter(). This method should create the Writer. I cloned Microsoft's method and added it to my Listener, and all seems to be okay. My Listener is suitable for a service now.

 internal bool EnsureWriter()
    {
        bool flag = true;
        if (Writer == null)
        {
            flag = false;
            if (baseFileName == null)
            {
                return flag;
            }
            Encoding encodingWithFallback = new UTF8Encoding(false);
            string fullPath = Path.GetFullPath(baseFileName);
            string directoryName = Path.GetDirectoryName(fullPath);
            string fileName = Path.GetFileName(fullPath);
            for (int i = 0; i < 2; i++)
            {
                try
                {
                    Writer = new StreamWriter(fullPath, true, encodingWithFallback, 0x1000);
                    flag = true;
                    break;
                }
                catch (IOException)
                {
                    fileName = Guid.NewGuid().ToString() + fileName;
                    fullPath = Path.Combine(directoryName, fileName);
                }
                catch (UnauthorizedAccessException)
                {
                    break;
                }
                catch (Exception)
                {
                    break;
                }
            }
            if (!flag)
            {
                baseFileName = null;
            }
        }
        return flag;
    }
mittio