灵活的登录界面设计在C# [英] Flexible Logging interface design in C#

查看:145
本文介绍了灵活的登录界面设计在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写我自己的日志记录类(在C#),它实现了一个标准接口,这是我可以从代码的任何部分调用。

I want to write my own Logging classes (in C#) which implement a standard interface, which I can call from any part of the code.

我的想法是有多个日志类实现记录仪的接口,分别用于其特定的日志目标,例如,FileLogger将实现日志记录到文件中,一个TextBox记录仪将实施登录到多行文本框的形式,一个DBLogger将实现日志记录到数据库表,等等。

My idea is to have multiple Log classes implement the Logger interface, each for its specific log destination, for example, a FileLogger will implement logging to a file, a TextBox logger will implement logging into a Multi Line TextBox in a Form, a DBLogger will implement logging to a database table, etc.

此外,每个记录器类可以有一个嵌套的记录器或链接记录器类,这样一个调用从应用程序代码log()方法可以登录多个目的地的消息; 。例如记录到文件,并在一个单一的调用方式的文本框

Further, each logger class can have a nested logger or chained logger classes, so that a single call to Log() method from the application code can log the message in multiple destinations; example log to a file and a textbox on Form in a single call.

我现在面临的困难是这样的:

The difficulty I am facing is this:

通常我登录到正在运行的日志文件(该文件将包含调试所需的所有日志信息),审查日志文件(仅包含日志消息由用户进行审查,或需要用户操作),多屏幕上的行文本框(这将复制所有日志消息给一个进度指示给用户),另有多行文本框(这将仅记录用户查看所需的信息)。

Usually I log to a running log file (which will contain all log messages required for debugging), a review log file (which will contain only log messages to be reviewed by the user, or which require user action), a Multi Line textbox on screen (which will replicate all log messages to give a progress indication to the user), and another Multi Line textbox (which will log only messages required for user to review).

当我打电话logger.Log(消息),一些消息可能并不适用于一个特定的日志目标。例如,一些消息可能会打算在运行日志文件或进度文本框仅记录,而不是在用户查看文本框,反之亦然。

When I call logger.Log(message), some messages may not apply to a particular log destination. For example, some message may be intended to be logged only in a running log file or progress textbox, but not in the user review textbox, and vice versa.

由于记录仪将被链接,这样一个函数调用就可以登录到所有需要的目的地,特定的记录如何识别日志消息不是为了它,从而忽略日志消息?

Since the loggers will be chained so that a single function call can log into all required destinations, how can a particular logger identify that the log message is not intended for it and hence ignore the log message?

我的示例日志接口是:

public interface Logger
{
    public void Log(string msg);
    public void Log(string msgType, string msg);
    public void InitLogSession();
    public void EndLogSession();
    public void AddLogger(Logger chainedLogger);
    public void RemoveLogger(Logger chainedLogger);
}

public class FileLogger : Logger
{
      //implement methods
}

public class TextBoxLogger : Logger
{
      //implement methods
}

public class DBLogger : Logger
{
      //implement methods
}

编辑1:

要更精确,可能有4伐木者:2文件记录器和2文本框记录器。一个特定的消息是假设意味着文本框记录器1,文件记录器1;我的设计应该如何处理这种

To be more precise, there could be 4 loggers: 2 file loggers and 2 textbox loggers. A particular message is suppose meant for 1 of the textbox loggers, and 1 of the file loggers; how should my design handle this?

编辑2:
请不要建议现有的日志框架。我只想写我自己

EDIT 2: Please do not suggest existing logging frameworks. I just want to write it on my own !

编辑3:
确认。我有一个设计。请给你的反馈,并可能填补国内空白。

EDIT 3: Ok. I have a design. Please give your feedback and probably fill the gaps.

修订后的界面:

public interface Logger
{
    public void Log(string msg);
    public void Log(string msgType, string msg);
    public void Log(int loggerIds, string msg);
    public void Log(int loggerIds, string msgType, string msg);
    public void InitLogSession();
    public void EndLogSession();
    public int getLoggerId();
}

public enum LoggerType
{
    File,
    TextBox
};

public class LoggerFactory
{
    public Logger getLogger(LoggerType loggerType)
    {

    }
}



的LoggerFactory类将实例化一个记录器的唯一途径。这个类将一个唯一的ID分配给记录仪的每个实例。这种独特的ID将是2。实施例的通电,第1次记录器将得到的id 1,第2将得到的id 2,第3将得到4,和第4次将得到8,等等。

The LoggerFactory class will be the sole way to instantiate a logger. This class will assign a unique id to each instance of a logger. This unique id will be a power of 2. Example, 1st logger will get id 1, 2nd will get id 2, 3rd will get 4, and 4th will get 8, and so on.

返回的记录器对象可强制转换为特定的类,而像文件路径,文本框等,进一步值可以通过设置来电,否则我可以有多种的LoggerFactory方法:一为每种类型的记录仪,这将接受特定的参数。

The returned logger object can be typecast to specific class, and further values like filePath, textbox, etc. can be set by the caller, or else I can have multiple methods in LoggerFactory: one for each type of logger, which will accept specific parameters.

因此,假设我们有4个记录仪与IDS 1,2,4,8。使用功能要记录
具有由第1和第3记录器(即记录器的ID 1和4)具有被处理的特定信息:

So, suppose we have 4 loggers with ids 1,2,4,8. A particular message which has to be processed by the 1st and 3rd logger (i.e. logger ids 1 and 4) has to be logged using the function:

    public void Log(int loggerIds, string msg);



的值被传递到loggerIds应为0101。每个记录器将检查其记录ID位是否为ON。如果是的,只有那么它会记录消息。

The value to be passed to loggerIds should be "0101". Each logger will check whether its logger id bit is ON. If yes, only then it will log the message.

现在在功能上签名,我刚才提到的int类型,但它是用于执行位manipuations特定的优化类型,比较?

Now in the function signatures, I have mentioned int type, but which is the specific optimised type for performing bit manipuations and comparisons?

在这种方法中,有大概可以上最大的限制没有。记录器,但就是没问题。请给你的反馈

In this approach, there can probably be a limit on the max no. of loggers, but that is fine with me. Please give your feedback.

注:目前我仍然在.NET 2.0。如果可能的话,建议在.NET 2.0解决方案,否则罚款,我可以移动到更高版本

Note: Currently I am still on .NET 2.0. If possible, suggest solution within .NET 2.0, else fine, I can move to higher versions.

这种设计的缺点:每个需要记录类,需要知道所有可用的记录器由应用程序实例化,并相应成立了位模式。任何想法如何有一个松耦合的设计?

CONS of this design: Each class which needs to log, needs to know about all the available loggers instantiated by the application, and accordingly set up the bit pattern. Any ideas how to have a loosely coupled design?

推荐答案

你为什么不看(或者实际上使用)的现有记录框架如 log4net的 NLOG

Why don't you look at (or indeed use) an existing logging framework such as log4net or NLog.

他们有一个日志级别(例如微量,信息,错误等)的概念,以及能够由日志的名称来过滤(这是通常其中调用日志调用的完全限定的类型名称)。然后,您可以将这些映射到一个或多个目标。

They have the concept of a log level (e.g. trace, info, error etc) as well as being able to filter by the name of the log (which is normally the fully qualified type name which invoked the logging call). You can then map these to one or more 'targets'.

这篇关于灵活的登录界面设计在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆