Home All Groups Group Topic Archive Search About

Tracking method calling

Author
14 Nov 2006 3:17 PM
stepanfryd
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();
    }
}

Author
14 Nov 2006 3:27 PM
Patrice
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.

--
Patrice

<stepanf***@msn.com> a écrit dans le message de news:
92C49EDA-1191-4F27-9A4D-C39B31638***@microsoft.com...
Show quote
> 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();
>    }
> }
>
>
>
>
>
>
>
Author
14 Nov 2006 4:26 PM
stepanfryd
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



Show quote
"Patrice" <scr***@chez.com> wrote in message
news:e4WyeFACHHA.1196@TK2MSFTNGP02.phx.gbl...
> 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.
>
> --
> Patrice
>
> <stepanf***@msn.com> a écrit dans le message de news:
> 92C49EDA-1191-4F27-9A4D-C39B31638***@microsoft.com...
>> 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();
>>    }
>> }
>>
>>
>>
>>
>>
>>
>>
>
>
Author
14 Nov 2006 6:51 PM
Patrice
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).

--
Patrice

<stepanf***@msn.com> a écrit dans le message de news:
B21E2E74-9E07-4780-9948-4C44CADDC***@microsoft.com...
Show quote
> 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
>
>
>
> "Patrice" <scr***@chez.com> wrote in message
> news:e4WyeFACHHA.1196@TK2MSFTNGP02.phx.gbl...
>> 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.
>>
>> --
>> Patrice
>>
>> <stepanf***@msn.com> a écrit dans le message de news:
>> 92C49EDA-1191-4F27-9A4D-C39B31638***@microsoft.com...
>>> 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();
>>>    }
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
Author
14 Nov 2006 6:30 PM
Michael Nemtsev
Hello stepanf***@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 Nietzsche

AddThis Social Bookmark Button