After reading an article on the codeproject about PostSharp and Log4Net.
So I tried to replicate that in VB.Net and hit a bump in the road. But here is how it got solved in the end.
So like the instructions say I downloaded PostSharp and installed it. I also downloaded the log4postsharp sourcecode.
Then I created a consoleapplication.
I set the references Log4PostSharp.dll and PostSharp.Public.dll (you can find this library on the “.NET” tab of the “Add reference…” dialog). And a reference to the log4net dll.
I then added some code to the module.
Imports Log4PostSharp
Module Module1
Sub Main()
Console.WriteLine(Add(1, 1))
Console.ReadLine()
Dim sum As Integer
sum = Add(1, 2)
Console.WriteLine(Add(1, 1))
Console.ReadLine()
End Sub
<Log(EntryLevel:=LogLevel.Debug, EntryText:="Adding {@i1} to {@i2}.", ExitLevel:=LogLevel.Debug, ExitText:="Result of addition is {returnvalue}.")> _
Private Function Add(ByVal i1 As Integer, ByVal i2 As Integer) As Integer
Return i1 + i2
End Function
End Module
Then I added an app.config file.
And added these log4net configurationsetting to it.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="MainAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MainAppender" />
</root>
</log4net>
</configuration>
But that did nothing, in the logging sense anyway.
So to make sure I didn’t make a mistake in the log4net configuration, I added this to the main method.
log4net.Config.BasicConfigurator.Configure()
Dim log As log4net.ILog = log4net.LogManager.GetLogger("logger-name")
log.Debug("test")
Console.ReadLine()
```
and that made the log line for me. Just the one, not the one from log4postsharp.
So I posted a question on the [postsharp forum][4]. And I got a reply the same day.
So I added this to the AssemblyInfo.vb
```xml
<Assembly: XmlConfigurator(Watch:=True)>
And then I removed these lines again
vbnet
log4net.Config.BasicConfigurator.Configure()
Dim log As log4net.ILog = log4net.LogManager.GetLogger("logger-name")
log.Debug("test")
Console.ReadLine()
and everything works as expected.