Maritim
Maritim

Reputation: 2159

NLog: Creating a program or service log

I have been using NLog for a couple of months now, just logging to the Event Log, however that is not at all ideal in any scenario imho.

I am wondering, is it possible to create your own Program or Service log with NLog? It doesn't seem like it according to the list of supported targets over at http://nlog-project.org/wiki/Targets

Is anyone able to shed some light on this?

Upvotes: 1

Views: 1144

Answers (1)

rootoz
rootoz

Reputation: 61

You can totally create an application who store and show you all the logs of your other applications, but you may need to extend NLog, that's what i've done for my workplace. For example :

  1. Create a generic database who can store all the logs from NLog and other types of log if needed.

  2. Make a custom Layout Renderer who create a XML with your log info (I don't try XmlEncode LayoutRenderer at the time I make my system, I think it'll be great now)

  3. Find a way to send your XML through your network, I used Transactionnal MSMQ but only the non-transactionnal target exists in NLog, so I created (=copy-paste the original with some enhancement) my own target

  4. Create a service who receive your log's XML and insert them in the database

  5. Finally, create an app who use the database to show you what happened in your environnement

It's a strong solution if you have the time to do it. For our production environnement it save us many times. But It depend on what you need exactly

* EDIT 2013-01-25 : *

For creating your own log in the Applications and Services logs tree : See this post who explain how to create your own log and the event source that goes with

Nlog can also create an event source if he doesn't exist, but as you can see in this post the application need Read permission for the key :

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Security

Or you can simply run the application as an administrator.

Once you have created your own log and his event source, you can now use Nlog normally, with a config like that :

<targets>
  <target xsi:type="EventLog" name="event" layout="${message}"
          machineName="." log="myLog" source="myEventSource"/>
</targets>
<rules>
  <logger name="myLogger" writeTo="event"/>
</rules>

Hope it helps

Upvotes: 2

Related Questions