Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« .Net has a Set implementation since the 3.5 framework. Use it.VB.Net: Single Instance application the better way »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

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.

  1. Imports Log4PostSharp
  2.  
  3. Module Module1
  4.  
  5.     Sub Main()
  6.         Console.WriteLine(Add(1, 1))
  7.         Console.ReadLine()
  8.         Dim sum As Integer
  9.         sum = Add(1, 2)
  10.         Console.WriteLine(Add(1, 1))
  11.         Console.ReadLine()
  12.     End Sub
  13.  
  14.     <Log(EntryLevel:=LogLevel.Debug, EntryText:="Adding {@i1} to {@i2}.", ExitLevel:=LogLevel.Debug, ExitText:="Result of addition is {returnvalue}.")> _
  15.   Private Function Add(ByVal i1 As Integer, ByVal i2 As Integer) As Integer
  16.         Return i1 + i2
  17.     End Function
  18.  
  19. End Module

Then I added an app.config file.
And added these log4net configurationsetting to it.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <configSections>
  4.     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  5.   </configSections>
  6.   <log4net>
  7.     <appender name="MainAppender" type="log4net.Appender.ConsoleAppender">
  8.       <layout type="log4net.Layout.PatternLayout">
  9.         <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
  10.       </layout>
  11.     </appender>
  12.     <root>
  13.       <level value="ALL" />
  14.       <appender-ref ref="MainAppender" />
  15.     </root>
  16.   </log4net>
  17. </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.

  1. log4net.Config.BasicConfigurator.Configure()
  2.         Dim log As log4net.ILog = log4net.LogManager.GetLogger("logger-name")
  3.         log.Debug("test")
  4.         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. And I got a reply the same day.

So I added this to the AssemblyInfo.vb

  1. <Assembly: XmlConfigurator(Watch:=True)>

And then I removed these lines again

  1. log4net.Config.BasicConfigurator.Configure()
  2.         Dim log As log4net.ILog = log4net.LogManager.GetLogger("logger-name")
  3.         log.Debug("test")
  4.         Console.ReadLine()

and everything works as expected.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
1395 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)