473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 936
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Schulz | last post by:
Hello, I'm using the logging package from python 2.3 on RH linux. Everything in the test program below works fine if we just use the log configuration with 'logging.basicConfig'. For the...
0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
6
by: Kevin Jackson | last post by:
Let's say we log exceptions to the windows application event log using the Exception Management Application Block. Is there a pattern that can be used so the exception is logged only once and not...
3
by: Udi | last post by:
Hi All, I'm developping an application that has a very strong logging requirement - we should be able to log up to 5000 messages per second, most of them are reletively small -(16 - 30 Words)...
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
5
by: Dilip | last post by:
I have a tiny design question to ask. I am stuck with a home-brewed logging library that has different methods for logging messages based on severity (like warning, error, information etc.). ...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
19
by: =?Utf-8?B?Sko=?= | last post by:
I have a logging component that I will access in other assemblies. So it was brought up to me that I should pass an instance around to these components instead of just making the logging class...
6
by: Larry Bates | last post by:
Every time I look at the logging module (up until now) I've given up and continue to use my home-grown logger that I've been using for years. I'm not giving up this time ;-) I find that I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.