Writing Custom Appender for NLog

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing Custom Appender for NLog

Mert Emin
Hi there,

I am trying to write a custom appender for NLog.

I appreciate any help or clarafication.

Best,
Mert Emin
Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Mert Emin
Like this tutorial tell (log4net)

Can I write a custom appender?

Yes. You can implement the log4net.Appender.IAppender interface to create you own customized appender. We recommend that you extend the log4net.Appender.AppenderSkeleton class rather than starting from scratch. You should implement your custom code in a assembly separate from the log4net assembly. To get started it is worth looking at the source of the log4net.Appender.TraceAppender as an example of the minimum amount of code required to get an appender working.

To configure log4net to use your custom appender you need to specify the assembly qualified name of the appender type in the config file. For example:

<appender name="..." type="MyNamespace.MyAppender, MyAssembly">
The .NET runtime will try to locate the assembly called MyAssembly. How .NET locates assemblies is beyond the scope of this FAQ.
Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Jarek Kowalski
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Mert Emin
Thanks for the link.

But there are some other issues. I am a mac user and under Example title it says "This is a skeleton target that writes messages to the specified host. Compile using:
csc.exe /t:library /out:MyAssembly.dll /r:NLog.dll MyFirstTarget.cs"

What I've done so far is creation of target class including TargetFactory.AddTarget(..) in its constructor, and config file mentioned on the page.

I tried to use ConfigurationItemFactory.Default.Targets.RegisterDefinition(...), but it gives an error.

Now, what?

Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Mert Emin
In reply to this post by Jarek Kowalski
Following the tutorial I successfully managed to use NLog with an config file.

To write custom target I changed configuration file as following:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <targets> 
                <target name="ANC" type="NLogAppender"/> 
        </targets> 
        <rules> 
                <logger name="*" minLevel="Trace" appendTo="ANC"/> 
        </rules> 
</nlog>

And I created an target class via "... public sealed class ANC : TargetWithLayout ..." I put "onfigurationItemFactory.Default.Targets.RegisterDefinition("ANC", typeof(Logging.NLogTarget.ANC));" into my main method and some other logging statements but I got the following error: ".../Logging/Main.cs(25,25): Error CS0103: The name `ConfigurationItemFactory' does not exist in the current context (CS0103) ()"

Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Jarek Kowalski
Administrator
Are you using NLog 2.0 or 1.0? The tutorial reflects the API for 2.0,
for 1.0 it was different.

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Writing Custom Appender for NLog

Mert Emin
I think it is 2.0. The folder I got NLog.dll file is called jkowalski-NLog-f488cd2.