471,348 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Logging messages in a class based on who is using it...?

Hi,
I'm not sure of the terminology here but...

I have a standalone assembly (Foo) and I log errors using a simple call
in my "catch" blocks: e.g.

class Foo
....
public DoSomething()
{
....
catch
{
LogError("blah", Severity.Error, etc....);
}
....
void LogError(msg,sev,...) { output.write(...) }
....
This assembly DLL will be referenced by other assemblies however each
of these calling assemblies may wish to log any errors their own way.
Can I make it so that the calling assembly somehow "overrides" the
LogError calls and writes the errors however it wishes. Ideally I'd
like:

using Foo
class Bar
....
Foo.DoSomething()
....
void LogError(msg) { showpopup(msg) } <--(I want THIS LogError to run
when DoSomething goes wrong)
I'm looking at interfaces, abstract classes, virtual functions, etc.
but I'm not inherting anything here - just making calls to an assembly.

Hope someone can help.

Thanks.

Nov 30 '05 #1
4 880
Hello JohnW,

A bit off-top, why do u want to use your own logger?
There are already a lot of nice realiziation of loggers - MS Enterprise,
log4net.

J> Hi,
J> I'm not sure of the terminology here but...
J> I have a standalone assembly (Foo) and I log errors using a simple
J> call in my "catch" blocks: e.g.
J>

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Nov 30 '05 #2
Hi,
Well that's the thing - I want calling apps to be able to use those
loggers if they want to. My own logger is just basic.

Thanks.

Nov 30 '05 #3
You could probably pass in the 'this' pointer to the log call, and have the
logging method check in the passed-in object for a specific interface, or
look for a specific class name in its assembly (or class with a specific
attribute) that will provide the overridden logging functionality. If it
doesn't find it, it just falls back to default behavior of writing to that
file. That's the least intrusive solution I can think of right now.

"JohnW" <jw******@email.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi,
I'm not sure of the terminology here but...

I have a standalone assembly (Foo) and I log errors using a simple call
in my "catch" blocks: e.g.

class Foo
...
public DoSomething()
{
...
catch
{
LogError("blah", Severity.Error, etc....);
}
...
void LogError(msg,sev,...) { output.write(...) }
...
This assembly DLL will be referenced by other assemblies however each
of these calling assemblies may wish to log any errors their own way.
Can I make it so that the calling assembly somehow "overrides" the
LogError calls and writes the errors however it wishes. Ideally I'd
like:

using Foo
class Bar
...
Foo.DoSomething()
...
void LogError(msg) { showpopup(msg) } <--(I want THIS LogError to run
when DoSomething goes wrong)
I'm looking at interfaces, abstract classes, virtual functions, etc.
but I'm not inherting anything here - just making calls to an assembly.

Hope someone can help.

Thanks.

Nov 30 '05 #4
You can use an event to allow client code to be notified whenever something
needs to be logged.
The example below shows a naïve implementation of this. You'll have to write
your own EventArgs implementation to contain the information to be logged.
public class MyClass
{
public void MyMethod()
{
try
{
throw new ApplicationException("Something went wrong");
}
catch
{
OnLog(new EventArgs());
}
}
private void OnLog(EventArgs e)
{
if (Logging!=null)
{
Logging(this,e);
}
}
public event LoggingEventHandler Logging;
public delegate void LoggingEventHandler(object sender, EventArgs e);
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 30 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Thomas Schulz | last post: by
reply views Thread by Robert.Schmitt | last post: by
6 posts views Thread by Kevin Jackson | last post: by
7 posts views Thread by Leo Breebaart | last post: by
19 posts views Thread by =?Utf-8?B?Sko=?= | last post: by
6 posts views Thread by Larry Bates | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.