Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using an event for logging a good idea?

Tags:

c#

.net

I'm working on program that is able to load various (self-made) plugins.

These Plugins need to have a way to send notifications to the host program.

Currently I have an Interface as a base for the plugins. The Interface defines an event that is fired whenever I need to log something

The Interface looks like this:

public delegate void LogEventHandler(object sender, LogEventArgs e);
public interface IWatcherPluginBase
{
    event LogEventHandler WriteLog;

    string Name { get; }
    string Description { get; }
    string Contact { get; }
    Version Version { get; }
    PluginState Status { get; }
    string StatusDescription { get; }
    void Start();
    void Stop();
}

In the plugin I use the following code to fire the event

if (WriteLog != null)
    WriteLog(this, new LogEventArgs("Started", MessageLevel.Info));

My main program adds an event handler

plugin.WriteLog += new LogEventHandler(plugin_WriteLog);

Do you know other (potentially better) ways to implement logging?

like image 903
peter Avatar asked Feb 26 '23 12:02

peter


1 Answers

You might consider usign built-in classes in the namespace System.Diagnostics (Debug, Debugger, Trace, EventLog).

That way you could redirect standard debug and trace output to VS console (when debugging) or to a file (when running a release build).

like image 166
data Avatar answered Mar 12 '23 22:03

data