473,745 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,se v,...) { 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 949
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******** *************@o 13g2000cwo.goog legroups.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,se v,...) { 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 ApplicationExce ption("Somethin g went wrong");
}
catch
{
OnLog(new EventArgs());
}
}
private void OnLog(EventArgs e)
{
if (Logging!=null)
{
Logging(this,e) ;
}
}
public event LoggingEventHan dler Logging;
public delegate void LoggingEventHan dler(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
2869
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 example below we get the expected output: ERROR:loggerName:error1 msg ERROR:loggerName:error2 msg
0
1659
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 and are not documented elsewhere. I used the logging configuration file shown below. My aim was to log only the INFO messages, but log DEBUG messages for one particular module (called webTestLogin.py).
6
9968
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 everytime up through the call chain? Is there anything a caller can key off of to check to see if it should also log the exception?
3
2068
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) while few of them might get to 1Kb in size. The data is binary (numbers) that should be converted to text of course. I'm affraid of a "logging-bottleneck" in my app.
7
1543
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. The situation is this: I am writing fairly large console scripts in Python. They have quite a few command-line options, which lead to configuration variables that are needed all over the program (e.g. the "--verbose" option alone is used by just...
5
1738
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.). Its very primitive, very simple and kind of gets the job done. The only drawback, I find myself littering my entire code base like this:
0
1863
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 want to put this comment and a alternative most simple default framework for discussion. Maybe there are more Python users which like to see that imported (managed) logging issue more down-to-earth and flexible ? ... Vinay Sajip wrote: >...
19
6074
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 static. If I make it static I wouldn't need to pass an instance around. What is your take on this?
6
7585
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 REALLY need to be able to monitor LOTS of running programs/processes and thought it would be nice to have them use SocketHandler logging and then I would write TCPServer to accept the log messages for real-time monitoring. I Googled (is that now a...
0
8970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8795
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9486
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8218
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3286
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.