Hi all,
I have a question about tracking of method calls. I would like to track/log
methods in code during runtime. I thought that I can write custom attribute
which I will use in method which call I want to log and in constructor of
the attribute I will write logging functionality. But this doesn't work. No
code in attribute class is executed.
Any idea how to solve this problem?
Stepan
---------------------------------------------------------
Example:
[AttributeUsage(AttributeTargets.Method)]
public class LoggerAttribute : System.Atribute {
public LoggerAttribute () {
// here is code for logging
}
}
Usage:
public class TestLoggerClass {
[Logger]
public void DoSomething() {
// here is some cod
}
}
public class TestProgram {
public static void Main(string[] params) {
TestLoggerClass x = new TestLoggerClass();
// I want to log method calling when I do next row. But it doesn't
work
x.DoSomething();
}
}
Patrice - 14 Nov 2006 15:27 GMT
Else have you checked when the attribute is initialized ? IMO it would be
rather look like an interception scheme that I believe can be done but
perhaps with unmanaged code if I remember (used for profilers ?). What
benefit do you expect from the custom attribute method ? For now, my first
thought would be likey to look at System.diagnostics.Trace capabilities.

Signature
Patrice
> Hi all,
>
[quoted text clipped - 36 lines]
> }
> }
stepanfryd@msn.com - 14 Nov 2006 16:26 GMT
Hi,
System.Diagnostics.Trace is similar. But I don't want to write code
"Trace.WriteBlahBlah()". I want write only Attribute on method and maximaly
specified trace category.
E.g.
[Logger("Category1")]
public void DoSomething1() {}
[Logger("Category2")]
public voiod DoSomething2() {}
I expect les code to use it. My idea is to use EntLibs Logging blog and
implemented it into attribute.
Stepan
> Else have you checked when the attribute is initialized ? IMO it would be
> rather look like an interception scheme that I believe can be done but
[quoted text clipped - 42 lines]
>> }
>> }
Patrice - 14 Nov 2006 18:51 GMT
In addition to Michael answer,
http://geekswithblogs.net/imilovanovic/articles/11589.aspx could be a
starting point to several ideas.
Make sure tracing calls is the only thing you'll need (else you'll liekly
need to go back to the "usual" trace mechanism).

Signature
Patrice
> Hi,
>
[quoted text clipped - 61 lines]
>>> }
>>> }
Michael Nemtsev - 14 Nov 2006 18:30 GMT
Hello stepanfryd@msn.com,
BTW, I recomment to look at this lib (free)
http://www.postsharp.org/
> I have a question about tracking of method calls. I would like to
> track/log methods in code during runtime. I thought that I can write
> custom attribute which I will use in method which call I want to log
> and in constructor of the attribute I will write logging
> functionality. But this doesn't work. No code in attribute class is
> executed.
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch