4 minutes to read read

With the introduction of OWIN  and self hosting, microsoft has really opened a lot of possible doors for developers and application users. This post is targetted to have an OWIN application hosted in Windows Service. Also we would be configuring the SSL for the Site.

Source code can be accessed at: GitHub

Creating a Windows Services Project

This is pretty straightforward. In visual Studio, Create new project and select Windows Service as project. This will add a project with default Service “Service1”. Rename to whatever the name expected.

Add OwinSelfHost and WebApi2

In the Tools-> Nuget package Manager -> PM console type in the following command to get the WebApi and self host packages referenced.

Install-Package Microsoft.Aspnet.WebApi.OwinSelfHost

This will install and reference all the required packages and dependencies. Also we need one more package to debug and use some functions like welcome package. For this import the  Microsoft.Owin.Diagnostics using PM Console.

Install-Package Microsoft.Owin.Diagnostics

Also, to Enable Cross Origing Resource Sharing, we need to import the following package:

Install-Package Microsoft.Owin.Cors

At this point the we are Good to Start.

Continue reading

3 minutes to read read

NLog Targets

The beauty of NLog is the Targets ‘system’ that is extentible and easy to use. Also the most amazing thing is the default target list as provided by NLog and the ease with which custom targets can be written. The Targets provided by the Nlog  by default are more then sufficient for most needs.

There can be multiple targets that can be linked to multiple Rules.

Well, the currently provided targets as provided by NLog can be accessed here. Some of the most useful targets with sample configuration is as below:

  1. ColoredConsole: This target adds a ‘beautiful’ Colored logs on the console. Well from the name it applies for Console Applications only.
    <targets  async="true">
       <target name="ConsoleTarget"  xsi:type="ColoredConsole"  layout="${longdate}, ${level}, ${message}"  useDefaultRowHighlightingRules="true"/>
  2. File Target: The file target as form the names writes the log to any file. Also, the location of the file could be on the network path as well.
        <targets  async="true">
          <target name="ConsoleTarget"  xsi:type="ColoredConsole"  layout="${longdate}, ${level}, ${message}"  useDefaultRowHighlightingRules="true"/>
          <target name="fileTarget"  xsi:type="File"  layout="${longdate}, ${level}, ${message}"               fileName="c:\temp\dummyLog_${longdate}.txt" />
          <target name="networkFilePath"  xsi:type="File"  layout="${longdate}, ${level}, ${message}"               fileName="\\\Shared\temp\dummyLog_${machinename}.txt" />

    There are two targets as defined above, one is normal file in local system and the other one is on network path. The archival options availabel for Files can be seen here
    There is also an possiblity to add more advanced options to control file parameters. An example code is given below as:

        <targets  async="true">
          <target name="adavancedFileTarget"  
                  layout="${longdate}, ${level}, ${message}"               
                  createDirs="true" />
  3. Debug Target: As the name suggests it writes to the Log sink for the attached debugger  (if any :))
        <targets  async="true">
          <target name="debugTarget"  xsi:type="Debugger" layout="${longdate}, ${level}, ${message}" />
  4. Trace Target:  This also as prev, send the messages to system.Diagnoistics.Trace Listener. This can be useful, if there are any existing trace listeners, and nlog is to be configured to integrate with them.
        <targets  async="true">
          <target name="traceTarget"  xsi:type="Trace" layout="${longdate}, ${level}, ${message}" />

Continue reading

2 minutes to read read

There are a plenty of logging tools and options in todays market but the userful are some. Almost all of them if not some do work on similar baseline, a set of rules to route and a sink to put in the logs. Some are really cool but have lost their sharpness with the low or no community updates like log4net which had been a great library for logging almost anything with .net. Some other good stuff like in the “Microsoft.Practices” namespace are also good but do have a good learning curve.

At this point NLog comes out as a wonderful logger with lowest learning curve, fastest adaption, small footprint, async support, and a large set of targets that can fulfill almost all requirement. With these set of articles, I shall try to put how easy it is to employe the hardlifting of logging in your projects. For this post, we shall take out lovely Simple Console Application. The download link for sample code can be found at the end of the post. Also note we are not using any abstract or patterns, this is just to demonstrate a brief purpose.

So, We first created the the Default Console Application Project with all default settings. And the we did a nuget install for nlog. This shall install the latest version of the package to project.

Install-Package NLog

Once we have this, we are pretty good to start. Second Step Continue reading