Windows services

Windows services applications have the same behavior as Console applications.

A typical windows service application has three events:

  • OnStart() - triggered when the service starts

  • OnStop() - triggered when the service stops

  • Execute() - the actual service implementation

We log the Execute() method by using a try-catch-finally block to simulate the BEGIN and the END of the action.

We call Logger.NotifyListeners(logger) (line 19) to ensure that the listeners will execute the OnFlush() event.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public partial class MyService : ServiceBase
{
    public void Execute(object source, ElapsedEventArgs e)
    {
        ILogger logger = new Logger(url: "MyService.Execute");

        try
        {
            logger.Info("executing MyService");

            // executing
        }
        catch(Exception ex)
        {
            logger.Error(ex);
        }
        finally
        {
            Logger.NotifyListeners(logger);
        }
    }
}

Full example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using KissLog;

namespace WindowsService_sample
{
    public partial class MyService : ServiceBase
    {
        private readonly Timer timer = new Timer();
        private readonly int _triggerInterval = 1000;

        private ILogger Logger = null;

        public MyService()
        {
            ConfigureKissLog();

            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Logger = new Logger();

            Logger.Info("***** Starting service *****");

            timer.Elapsed += new ElapsedEventHandler(Execute);
            timer.Interval = _triggerInterval;
            timer.Enabled = true;
        }

        protected override void OnStop()
        {
            Logger.Info("Service stopped");

            timer.Stop();
        }

        public void Execute(object source, ElapsedEventArgs e)
        {
            ILogger logger = new Logger(url: "MyService.Execute");

            try
            {
                logger.Info("executing MyService");

                // executing
            }
            catch(Exception ex)
            {
                logger.Error(ex);
            }
            finally
            {
                KissLog.Logger.NotifyListeners(logger);
            }
        }

        private void ConfigureKissLog()
        {
            string organizationId = "0337cd29-a56e-42c1-a48a-e900f3116aa8";
            string applicationId = "b1d65503-fc9d-4d3d-9f37-3c8be9fcb450";

            // KissLog.net cloud listener
            KissLogConfiguration.Listeners.Add(new KissLogApiListener(new KissLog.Apis.v1.Auth.Application(organizationId, applicationId))
            {
                UseAsync = false
            });

            // local text file listener
            KissLogConfiguration.Listeners.Add(new LocalTextFileListener(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"))
            {
                FlushTrigger = FlushTrigger.OnMessage
            });
        }
    }
}
Windows services logs on KissLog.net

Windows services logs on KissLog.net

Windows services on text file

Windows services logs on text file