Tracking method calling

G

Guest

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();
}
}
 
P

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.
 
G

Guest

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 said:
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

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();
}
}
 
M

Michael Nemtsev

Hello (e-mail address removed),

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
 
P

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

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 said:
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

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();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top