log4net using Laos

Share with us how you use PostSharp, or how you would like to use it.

log4net using Laos

Postby morphine on Wed Jun 11, 2008 10:34 pm

Hi Gael,

I've been using PostSharp Laos for some time now, it's very cool! I'm trying to use it with log4net, to provide a way to easily log whatever is needed.

I've seen log4PostSharp, and I cannot use it for my purpose. Here's what I want to achieve:
  • provide a configurable attribute at class level: LoggableContext (done)
  • enforce the user to define 2 fields in that class: an log4net.ILog Log and a string LoggerName, both static; failure to comply yields compile-time errors (done)
  • user has the possibility to define a DefaultLoggerName when applying attribute; see code below for example (done too)
  • if and only if the DefaultLoggerName was set, I don't want the user to be able to set the field LoggerName, ever: for such an attempt a warning or an error should be issued; and this is where I need your help: is there any way to make this happen? (except for throwing exceptions at runtime...)
I guess I want to use PoshSharp for code analysis, eh?

Code: Select all
[LoggableContext(DefaultLoggerName="MyLogger")]
public class MyClass
{
   private static readonly ILog Log;
   public static readonly string LoggerName = "YourLogger";

   public void DoSomething()
   {
      Log.Info("Yeah yeah...doing some stuff.");
      // Log is not initialized in MyClass, as this is the job of the attribute, so this code will not fail
   }
}


Best,
Dan
morphine
 
Posts: 6
Joined: Sat Jun 07, 2008 10:16 am
First Name: Dan
Last Name: M.

Re: log4net using Laos

Postby gfraiteur on Wed Jun 11, 2008 10:40 pm

Wouldn't it be better to require the field LoggerName to be constant? So you would have the guarantee it is never updated...
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 816
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: Coding Glove

Re: log4net using Laos

Postby morphine on Wed Jun 11, 2008 10:47 pm

Just tried; I'm using OnFieldAccessAspects and it looks like they don't work with constant fields...

Besides, I have the same requirement for Log: this one must never be initialized by user.
morphine
 
Posts: 6
Joined: Sat Jun 07, 2008 10:16 am
First Name: Dan
Last Name: M.

Re: log4net using Laos

Postby gfraiteur on Wed Jun 11, 2008 11:09 pm

Sure, OnFieldAccessAspects does not make sense with constant fields. But you can use a type-level aspect (like Compound) to verify all fields.

It's true it does not solve the problem of Log...

What you want is actually possible... using PostSharp Core. A "little" more difficult, however!
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 816
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: Coding Glove

Re: log4net using Laos

Postby morphine on Wed Jun 11, 2008 11:27 pm

Yes, I am using CompoundAspect :) I must be on the right track then!

Hm..Core; I'll have a look at it one day. Thanks for the hint!

Btw: when using a MessageSource to display messages, I noticed Write method has these params:

Code: Select all
string locationFile, int locationLine, int locationColumn

I suppose I need to supply this data from PDB, right? The question is: does PostSharp have support for reading PDB files?
morphine
 
Posts: 6
Joined: Sat Jun 07, 2008 10:16 am
First Name: Dan
Last Name: M.

Re: log4net using Laos

Postby gfraiteur on Thu Jun 12, 2008 7:12 am

It has, but in Core, not in Laos.
Gael Fraiteur, project leader
gfraiteur
Site Admin
 
Posts: 816
Joined: Tue Dec 18, 2007 3:09 pm
First Name: Gael
Last Name: Fraiteur
Company: Coding Glove


Return to What To Do With PostSharp?