Partager via


Creating a custom out-of-process event listener host application

patterns & practices Developer Center

An Out-of-Process Host application named SemanticLogging.Etw.WindowsService is available for use with the Semantic Logging Application Block. You can use this to collect and manage trace messages generated by another process. If the supplied Out-of-Process Host application does not meet your requirements, you can create your own out-of-process trace message collector application by following the approach used in the Out-of-Process Host application.

You can run the Out-of-Process Host application either as a Windows service or as a simple console application. Internally, both of these use the TraceEventService and TraceEventServiceConfiguration classes. You can create your own custom out-of-process event listener using these same two classes. The following code sample from the out-of-process console host illustrates their use.

internal class TraceEventServiceHost
{
  private const string LoggingEventSourceName = "Logging";
  private const string EtwConfigurationFileName = "EtwConfigurationFileName";

  private static readonly TraceSource logSource = new TraceSource(LoggingEventSourceName);

  internal static void Run()
  {
    try
    {
      ...
      var configuration = TraceEventServiceConfiguration.Load(
        ConfigurationManager.AppSettings[EtwConfigurationFileName]);
      using (var service = new TraceEventService(configuration))
      {
        service.Start();
        ...
      }
    }
    catch (Exception e)
    {    ...    }
    finally
    {
      logSource.Close();
    }
  }
  ...
}

You should consider adding a mechanism that monitors the configuration file for changes. The Out-of-Process Host application included with the block uses the FileSystemWatcher class from the .NET Framework to monitor the configuration file for changes. When it detects a change, it automatically restarts the internal TraceEventService instance with the new configuration settings.

For more information about the structure of the XML file that configures the TraceEventService class, see the topic Configuration schema for the out-of-process model.

To recycle the service if the trace event settings change, you can use the following code.

private void OnTraceEventServiceSettingsChanged(
  object sender, PropertyChangedEventArgs e)
{
  this.OnStop();
  this.OnStart(null); 
}

You must run the application that hosts the TraceEventService class under a user account that has the minimum required permissions. For more information see Installing and running the Out-of-Process Windows Service/Console Host.

You should consider using a setting in the app.config file to specify the name and location of the XML configuration file for the TraceEventService.

Next Topic | Previous Topic | Home | Community