public void FlightRecorder_PassThru_SysLogProvider() { // Verify that the ISysLogProvider implementation works. Queue <FlightEvent> queue = new Queue <FlightEvent>(); ISysLogProvider orgProvider = SysLog.LogProvider; try { FlightEvent flightEvent; using (var recorder = new FlightRecorder(evt => queue.Enqueue(evt))) { SysLog.LogProvider = recorder; SysLog.LogError("Test Error"); SysLog.LogWarning("Test Warning"); SysLog.LogInformation("Test Information"); SysLog.Flush(); Assert.AreEqual(3, queue.Count); flightEvent = queue.Dequeue(); Assert.AreEqual("SysLog:Error", flightEvent.Operation); Assert.IsTrue(flightEvent.Details.Contains("Test Error")); Assert.IsTrue(flightEvent.IsError); flightEvent = queue.Dequeue(); Assert.AreEqual("SysLog:Warning", flightEvent.Operation); Assert.IsTrue(flightEvent.Details.Contains("Test Warning")); Assert.IsFalse(flightEvent.IsError); flightEvent = queue.Dequeue(); Assert.AreEqual("SysLog:Information", flightEvent.Operation); Assert.IsTrue(flightEvent.Details.Contains("Test Information")); Assert.IsFalse(flightEvent.IsError); // Verify that system events actually serialize exception // and stack trace related information. try { throw new AssertException(); } catch (Exception e) { SysLog.LogException(e); SysLog.Flush(); flightEvent = queue.Dequeue(); Assert.AreEqual("SysLog:Exception", flightEvent.Operation); Assert.IsTrue(flightEvent.Details.Contains("AssertException")); } } } finally { SysLog.LogProvider = orgProvider; } }
//--------------------------------------------------------------------- // IServiceHost implementations /// <summary> /// Initializes the service user interface. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="service">The service to associate with this instance.</param> /// <param name="logProvider">The optional system log provider (or <c>null</c>).</param> /// <param name="start"><c>true</c> to start the service.</param> public void Initialize(string[] args, IService service, ISysLogProvider logProvider, bool start) { this.logProvider = logProvider; this.args = args; this.service = service; this.autoStart = start; }
//--------------------------------------------------------------------- // IServiceHost implementations /// <summary> /// Initializes the service user interface. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="service">The service to associate with this instance.</param> /// <param name="logProvider">The optional system log provider (or <c>null</c>).</param> /// <param name="start"><c>true</c> to start the service.</param> public void Initialize(string[] args, IService service, ISysLogProvider logProvider, bool start) { this.args = args; this.service = service; if (start) { Start(); } }
/// <summary> /// Instantiates a service host that can be explicitly controlled by the application. /// </summary> /// <param name="service">The service instance.</param> /// <param name="logProvider">Specifies the system log provider (or <c>null</c>).</param> /// <param name="args">The command line arguments to be passed to the service (or <c>null</c>).</param> public ServiceHost(IService service, ISysLogProvider logProvider, string[] args) { if (logProvider == null) { logProvider = new NativeSysLogProvider(service.Name); } this.service = service; this.logProvider = logProvider; this.args = args != null ? args : new string[0]; }
/// <summary> /// Adds a new log provider to the collection. /// </summary> /// <param name="provider">The new provider.</param> /// <exception cref="ArgumentNullException">Thrown if the provider passed is <c>null</c>.</exception> public void Add(ISysLogProvider provider) { if (provider == null) { throw new ArgumentNullException("provider"); } lock (syncLock) { this.providers.Add(provider); } }
//--------------------------------------------------------------------- // IServiceHost implementations /// <summary> /// Initializes the service user interface. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="service">The service to associate with this instance.</param> /// <param name="logProvider">The optional system log provider (or <c>null</c>).</param> /// <param name="start"><c>true</c> to start the service.</param> public void Initialize(string[] args, IService service, ISysLogProvider logProvider, bool start) { if (logProvider != null) { SysLog.LogProvider = logProvider; } else { SysLog.LogProvider = new NativeSysLogProvider(service.Name); } this.service = service; }
/// <summary> /// Initializes the service user interface. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="service">The service to associate with this instance.</param> /// <param name="logProvider">The optional system log provider (or <c>null</c>).</param> /// <param name="start"><c>true</c> to start the service.</param> /// <remarks> /// If a non-<c>null</c> <paramref name="logProvider" /> is passed then it will be /// setup as the global log provider. If <c>null</c> is passed then the service /// host will initialize a provider that logs events to the FreeSWITCH logging /// subsystem. /// </remarks> public void Initialize(string[] args, IService service, ISysLogProvider logProvider, bool start) { this.service = service; if (logProvider == null) { SysLog.LogProvider = new SwitchLogProvider(); } if (start) { Service.Start(this, args); } }
private IService service; // The associated service instance /// <summary> /// Initializes the service user interface. /// </summary> /// <param name="args">The command line arguments.</param> /// <param name="service">The service to associate with this instance.</param> /// <param name="logProvider">The optional system log provider (or <c>null</c>).</param> /// <param name="start"><c>true</c> to start the service.</param> public void Initialize(string[] args, IService service, ISysLogProvider logProvider, bool start) { if (logProvider != null) { SysLog.LogProvider = logProvider; } this.service = service; if (start) { try { service.Start(this, args); } catch (Exception e) { SysLog.LogException(e); } } }
/// <summary> /// Runs the service passed as either a native service or a Windows /// forms application, depending on the command line arguments /// passed. /// </summary> /// <param name="service">The service instance.</param> /// <param name="logProvider">Specifies the system log provider (or <c>null</c>).</param> /// <param name="args">The command line arguments.</param> /// <remarks> /// <para> /// This method intreprets the following command-line arguments: /// "-mode" and "-start". /// </para> /// <para> /// The -mode parameter can be passed as: /// </para> /// <code language="none"> /// -mode:console Runs as a console application /// -mode:service Runs as a native Windows service /// -mode:form Runs as a Windows Forms application /// </code> /// <note> /// Windows CE supports only -mode:console and any /// other parameter will be ignored. If none of these are /// passed then -mode:service will be assumed. /// </note> /// <para> /// The "-start" argument is necessary only if the service /// is running as a Windows form application. If present, this /// indicates that the service should be started automatically /// by this method. /// </para> /// <para> /// The <paramref name="logProvider" /> parameter can be used to override the default /// system log provider implementation which will be provided /// if <paramref name="logProvider" /> as <c>null</c>. By default, services hosted as a Windows Form /// will direct the system log to a control on the form as well as the /// Windows event log. Native and internal services will use the /// Windows event log, and console services will discard log postings by /// default. /// </para> /// </remarks> public static void Run(IService service, ISysLogProvider logProvider, string[] args) { var mode = Mode.Service; var autoStart = false; foreach (string arg in args) { switch (arg.ToLowerInvariant()) { case "-mode:console": mode = Mode.Console; break; case "-mode:service": mode = Mode.Service; break; case "-mode:form": mode = Mode.Form; break; case "-start": autoStart = true; break; } } switch (mode) { case Mode.Console: { var host = new ConsoleServiceHost(); host.Initialize(args, service, logProvider, true); service.Start(host, args); host.WaitForStop(); break; } case Mode.Service: { var host = new NativeServiceHost(); System.ServiceProcess.ServiceBase[] services = new System.ServiceProcess.ServiceBase[] { host }; host.Initialize(args, service, logProvider, false); System.ServiceProcess.ServiceBase.Run(services); break; } case Mode.Form: { var host = new FormServiceHost(); host.Initialize(args, service, logProvider, autoStart); Application.Run(host); break; } } }