使用数据库作为存储介质在.NET中实现日志记录库 [英] Implementing a logging library in .NET with a database as the storage medium

查看:161
本文介绍了使用数据库作为存储介质在.NET中实现日志记录库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是开始工作在日志库,每个人都可以使用来跟踪任何类型的系统信息,而用户正在运行我们的应用程序。到目前为止,最简单的示例是跟踪信息,警告和错误。

I'm just starting to work on a logging library that everyone can use to keep track of any sort of system information while the user is running our application. The simplest example so far is to track Info, Warnings, and Errors.

我希望所有插件都能使用此功能,但由于每个开发人员可能有不同的

I want all plugins to be able to use this feature, but since each developer might have a different idea of what's important to report, I want to keep this as generic as possible.

在C ++世界中,我通常使用类似于的方式, stl :: pair< string,string> 作为键值对结构,并有 stl :: list 在日志中的行。日志缓存将是 list< list< pair< string,string>>> (ugh!这样,开发人员可以使用诸如INFO,WARNING,ERROR之类的常量字符串键,对数据库中的列具有一致的命名(用于选择特定类型的信息)。

In the C++ world, I would normally use something like a stl::pair<string,string> to act as a key value pair structure, and have a stl::list of these to act as a "row" in the log. The log cache would then be a list<list<pair<string,string>>> (ugh!). This way, the developers can use a const string key like INFO, WARNING, ERROR to have a consistent naming for a column in the database (for SELECTing specific types of information).

我希望数据库能够处理任何数量的不同列名。例如,John可能有一个INFO行带有一个名为USER的列,而Bill可能有一个INFO行带有一个名为FILENAME的列。我希望日志查看器能够显示所有信息,如果一个报表没有INFO / FILENAME的值,这些字段应该只显示为空白。所以一个选择是使用 List< List< KeyValuePair< String,String>> >,另一个是使日志库消费者以某种方式注册然后让数据库做一个 ALTER TABLE 来处理这种情况。另一个想法是有一个表只是为键值对,用外键将键值对映射回原始日志条目。

I'd like the database to be able to deal with any number of distinct column names. For example, John might have an INFO row with a column called USER, and Bill might have an INFO row with a column called FILENAME. I want the log viewer to be able to display all information, and if one report doesn't have a value for INFO / FILENAME, those fields should just appear blank. So one option is to use List<List<KeyValuePair<String,String>>>, and the another is to have the log library consumer somehow "register" its schema, and then have the database do an ALTER TABLE to handle this situation. Yet another idea is to have a table that's just for key value pairs, with a foreign key that maps the key value pairs back to the original log entry.

我明显don不希望日志记录浪费系统,所以我只锁定日志缓存以制作数据的副本(并删除已复制的数据),然后后台线程将信息转储到数据库。

I obviously don't want logging to bog down the system, so I only lock the log cache to make a copy of the data (and remove the already-copied data), then a background thread will dump the information to the database.

我的具体问题是:


  1. 你看到任何性能问题吗?换句话说,你曾经试过这样的事情,发现某些事情在实践中不能很好地工作吗?

  2. 有更多的 .NETish 方式以便实现除 List< List< KeyValuePair< String,String>>>

  3. 之外的键值对。有一种方法可以做#2更好,是ALTER TABLE的想法我提出上面一个坏事?

  4. 你会推荐多个数据库一个单一的?我还不知道日志写入的频率,但我们希望有很多低级别的信息。也许应该有一个DB只有一个固定的架构只为低级的东西,然后是另一个DB,更灵活的向用户报告信息。

  1. Do you see any performance issues? In other words, have you ever tried something like this and found that certain things just don't work well in practice?
  2. Is there a more .NETish way to implement the key value pairs, other than List<List<KeyValuePair<String,String>>>?
  3. Even if there is a way to do #2 better, is the ALTER TABLE idea I proposed above a Bad Thing?
  4. Would you recommend multiple databases over a single one? I don't yet have an idea of how frequently the log would get written to, but we ideally would like to have lots of low level information. Perhaps there should be a DB with a fixed schema only for the low level stuff, and then another DB that's more flexible for reporting information back to users.


推荐答案

为什么不检查 log4net ?它可能已经足够你的目的,你会避免重新发明轮已经发明了很多次: - )

why don't you check log4net? It may be enough for your purposes and you would avoid re invent a wheel already invented many times :-)

这里有一些配置示例,如何存储日志信息在数据库上:

Here you have some configuration examples about how to store the logging information on the database:

http://logging.apache.org/log4net/release/config-examples.html

这篇关于使用数据库作为存储介质在.NET中实现日志记录库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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