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

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

问题描述

我刚开始对每个人都可以用它来保持任何形式的系统信息的跟踪,而用户正在运行我们的应用程序日志记录库工作。最简单的例子,到目前为止是跟踪信息,警告和错误。



我想所有的插件才能使用此功能,但由于每个开发者可能有不同的什么是重要的思想汇报,我想保持这种尽可能通用。



在C ++的世界里,我通常会使用的东西就像一个 STL ::对<字符串,字符串> 来作为一个键值对结构,并有一个 STL ::列表这些为行动在日志中行。然后,日志高速缓存将是列表<清单及LT;对<字符串,字符串>>> (啊!)。这样,开发人员可以使用像INFO,WARNING,ERROR一个常量字符串键,以在数据库中的列一致的命名(选择特定类型的信息)。



我想数据库能够处理任何数量的不同列名。例如,约翰可能有一列名为用户的INFO行,比尔可能有一列名为filename的INFO一行。我想日志查看器能够显示所有信息,如果报告没有对信息/文件名的值,这些领域应该只是显示为空白。因此,一个选择是使用列表<名单< KeyValuePair<字符串,字符串>> >,而另一种是有日志库消费者有点注册的模式,然后对数据库做一个 ALTER TABLE 来处理这种情况。然而,另一个想法是有一个表,只是为了键值对,与映射键值对回原来的日志条目的外键。



我明明唐不想登录陷入瘫痪系统,所以我只锁定日志缓存,使数据的副本(并删除已复制的数据),那么后台线程将转储信息到数据库中。



我对有关具体问题这个是:




  1. 请你看到任何性能问题?换句话说,你有没有尝试过这样的事情,并发现某些事情根本不能很好地在实践中?工作

  2. 是否有更多的 .NETish 办法实施键值对,比其他列表<名单< KeyValuePair<字符串,字符串>>>

  3. 即便?有没有办法做到#2更好,就是我上面坏事?

  4. 你会推荐多个数据库在一个单一提出了ALTER TABLE想法?我还没有怎么频繁的日志将被写入到一个想法,但我们最好希望有大量的低级别的信息。也许应该有一个固定的模式只适用于低层次的东西一个数据块,然后又DB这是向用户报告信息回到更加灵活。


解决方案

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



在这里,您对如何存储日志信息的一些配置范例在数据库上:



的http://日志.apache.org / log4net的/发布/配置-examples.html


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.

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).

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.

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.

My specific questions regarding this are:

  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.

解决方案

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天全站免登陆