简体   繁体   English

使用SQL Server进行应用程序日志记录。 优点缺点?

[英]Using a SQL Server for application logging. Pros/Cons?

I have a multi-user application that keeps a centralized logfile for activity. 我有一个多用户应用程序,它为活动保留了一个集中的日志文件。 Right now, that logging is going into text files to the tune of about 10MB-50MB / day. 现在,日志记录进入文本文件大约10MB-50MB /天。 The text files are rotated daily by the logger, and we keep the past 4 or 5 days worth. 记录器每天轮换文本文件,我们保留过去4或5天的价值。 Older than that is of no interest to us. 比这更老的我们没兴趣。

They're read rarely: either when developing the application for error messages, diagnostic messages, or when the application is in production to do triage on a user-reported problem or a bug. 它们很少被阅读:在开发应用程序以获取错误消息,诊断消息时,或者在应用程序处于生产状态时,对用户报告的问题或错误进行分类。

(This is strictly an application log. Security logging is kept elsewhere.) (这严格来说是应用程序日志。安全日志记录保存在别处。)

But when they are read, they're a pain in the ass. 但是当他们被阅读时,他们就是痛苦的屁股。 Grepping 10MB text files is no fun even with Perl: the fields (transaction ID, user ID, etc..) in the file are useful, but just text. 即使使用Perl,使用10MB文本文件也很有趣:文件中的字段(事务ID,用户ID等)很有用,但只是文本。 Messages are written sequentially, one like at a time, so interleaved activity is all mixed up when trying to follow a particular transaction or user. 消息是按顺序写入的,一次一个地写入,因此当尝试跟踪特定事务或用户时,交错活动全部混淆。

I'm looking for thoughts on the topic. 我正在寻找关于这个主题的想法。 Anyone done application-level logging with an SQL database and liked it? 有人用SQL数据库完成了应用程序级日志记录并喜欢它吗? Hated it? 讨厌吗?

I think that logging directly to a database is usually a bad idea, and I would avoid it. 我认为直接记录到数据库通常是一个坏主意,我会避免它。

The main reason is this: a good log will be most useful when you can use it to debug your application post-mortem, once the error has already occurred and you can't reproduce it. 主要原因是:一旦错误已经发生并且您无法重现它,当您可以使用它来验证您的应用程序验证时,一个好的日志将是最有用的。 To be able to do that, you need to make sure that the logging itself is reliable. 为了能够做到这一点,您需要确保日志记录本身是可靠的。 And to make any system reliable, a good start is to keep it simple. 为了使任何系统可靠,一个良好的开端是保持简单。

So having a simple file-based log with just a few lines of code (open file, append line, close file or keep it opened, repeat...) will usually be more reliable and useful in the future, when you really need it to work. 因此,只需几行代码(打开文件,追加行,关闭文件或保持打开,重复...)的简单基于文件的日志,将来通常会更加可靠和有用,当您真正需要它时上班。

On the other hand, logging successfully to an SQL server will require that a lot more components work correctly, and there will be a lot more possible error situations where you won't be able to log the information you need, simply because the log infrastructure itself won't be working. 另一方面,成功记录到SQL服务器将需要更多组件正常工作,并且会有更多可能的错误情况,您将无法记录所需的信息,只是因为日志基础结构本身不会起作用。 And something worst: a failure in the log procedure (like a database corruption or a deadlock) will probably affect the performance of the application, and then you'll have a situation where a secondary component prevents the application of performing it's primary function. 最糟糕的事情:日志过程中的失败(如数据库损坏或死锁)可能会影响应用程序的性能,然后您将遇到辅助组件阻止应用程序执行其主要功能的情况。

If you need to do a lot of analysis of the logs and you are not comfortable using text-based tools like grep, then keep the logs in text files, and periodically import them to an SQL database. 如果您需要对日志进行大量分析,并且不习惯使用基于文本的工具(如grep),请将日志保存在文本文件中,并定期将其导入SQL数据库。 If the SQL fails you won't loose any log information, and it won't even affect the application's ability to function. 如果SQL失败,您将不会丢失任何日志信息,甚至不会影响应用程序的运行能力。 Then you can do all the data analysis in the DB. 然后,您可以在数据库中进行所有数据分析。

I think those are the main reasons why I don't do logging to a database, although I have done it in the past. 我认为这些是我不登录数据库的主要原因,尽管我过去曾这样做过。 Hope it helps. 希望能帮助到你。

We used a Log Database at my last job, and it was great. 我在上一份工作中使用了一个日志数据库,这很棒。

We had stored procedures that would spit out overviews of general system health for different metrics that I could load from a web page. 我们有一些存储过程会针对我可以从网页加载的不同指标吐出一般系统运行状况的概述。 We could also quickly spit out a trace for a given app over a given period, and if I wanted it was easy to get that as a text file, if you really just like grep-ing files. 我们还可以在给定的时间段内快速为给定的应用程序吐出一条跟踪,如果我想要将它作为文本文件很容易,那么如果你真的喜欢grep-ing文件。

To ensure the logging system does not itself become a problem, there is of course a common code framework we used among different apps that handled writing to the log table. 为了确保日志记录系统本身不会成为问题,我们当然会在处理写入日志表的不同应用程序中使用公共代码框架。 Part of that framework included also logging to a file, in case the problem is with the database itself, and part of it involves cycling the logs. 该框架的一部分包括记录到文件,以防问题出在数据库本身,其中一部分涉及循环日志。 As for the space issues, the log database is on a different backup schedule, and it's really not an issue. 至于空间问题,日志数据库采用不同的备份计划,这确实不是问题。 Space (not-backed-up) is cheap. 空间(不备份)很便宜。

I think that addresses most of the concerns expressed elsewhere. 我认为这解决了其他地方表达的大部分问题。 It's all a matter of implementation. 这都是实施的问题。 But if I stopped here it would still be a case of "not much worse", and that's a bad reason to go the trouble of setting up DB logging. 但是,如果我在这里停止,它仍然是“不会更糟”的情况,这是设置数据库日志记录的麻烦的一个坏理由。 What I liked about this is that it allowed us to do some new things that would be much harder to do with flat files. 我喜欢这个是它允许我们做一些新的事情 ,这对平面文件更难。

There were four main improvements over files. 文件有四个主要改进。 The first is the system overviews I've already mentioned. 首先是我已经提到的系统概述。 The second, and imo most important, was a check to see if any app was missing messages where we would normally expect to find them. 第二个,也是最重要的,是检查是否有任何应用程序丢失了我们通常希望找到它们的消息。 That kind of thing is near-impossible to spot in traditional file logging unless you spend a lot of time each day reviewing mind-numbing logs for apps that just tell you everything's okay 99% of the time. 在传统的文件记录中几乎不可能发现这种情况,除非你每天都花费大量时间来审查那些只会告诉你99%的时间都没问题的应用程序。 It's amazing how freeing the view to show missing log entries is. 令人惊讶的是如何释放视图以显示缺少的日志条目。 Most days we didn't need to look at most of the log files at all... something that would be dangerous and irresponsible without the database. 大多数时候我们根本不需要查看大多数日志文件......如果没有数据库,这将是危险和不负责任的。

That brings up the third improvement. 这带来了第三个改进。 We generated a single daily status e-mail, and it was the only thing we needed to review on days that everything ran normally. 我们生成了一个日常状态电子邮件,这是我们在一切正常运行的日子里唯一需要查看的内容。 The e-mail included showed errors and warnings. 包含的电子邮件显示错误和警告。 Missing logs were re-logged as warning by the same db job that sends the e-mail, and missing the e-mail was a big deal. 丢失的日志被发送电子邮件的同一个db作业重新记录为警告,并且丢失电子邮件是一件大事。 We could send forward a particular log message to our bug tracker with one click, right from within the daily e-mail (it was html-formatted, pulled data from a web app). 我们可以在每日电子邮件中点击一下,向我们的错误跟踪器发送特定的日志消息(它是html格式的,从Web应用程序中提取数据)。

The final improvement was that if we did want to follow a specific app more closely, say after making a change, we could subscribe to an RSS feed for that specific application until we were satisfied. 最后的改进是,如果我们确实想要更密切地关注特定的应用程序,比如在做出更改之后,我们可以订阅该特定应用程序的RSS提要,直到我们满意为止。 It's harder to do that from a text file. 从文本文件中更难做到这一点。

Where I'm at now, we rely a lot more on third party tools and their logging abilities, and that means going back to a lot more manual review. 我现在所处的位置,我们更多地依赖于第三方工具及其日志记录功能,这意味着要回到更多的人工审核。 I really miss the DB, and I'm contemplated writing a tool to read those logs and re-log them into a DB to get these abilities back. 我真的很想念数据库,我打算编写一个工具来读取这些日志并将它们重新登录到数据库中以恢复这些功能。

Again, we did this with text files as a fallback, and it's the new abilities that really make the database worthwhile. 我们再次使用文本文件作为后备,这是真正使数据库值得的新功能。 If all you're gonna do is write to a DB and try to use it the same way you did the old text files, it adds unnecessary complexity and you may as well just use the old text files. 如果您要做的就是写入数据库并尝试以与旧文本文件相同的方式使用它,这会增加不必要的复杂性,您也可以使用旧的文本文件。 It's the ability to build out the system for new features that makes it worthwhile. 它能够为新功能构建系统,使其变得有价值。

yeah, we do it here, and I can't stand it. 是的,我们在这里做,我无法忍受。 One problem we have here is if there is a problem with the db (connection, corrupted etc), all logging stops. 我们遇到的一个问题是如果db(连接,损坏等)出现问题,所有日志记录都会停止。 My other big problem with it is that it's difficult to look through to trace problems. 我的另一个大问题是难以查看跟踪问题。 We also have problems here with the table logs taking up too much space, and having to worry about truncating them when we move databases because our logs are so large. 我们这里也遇到问题,表日志占用了太多空间,并且由于我们的日志太大而不得不担心在移动数据库时截断它们。

I think its clunky compared to log files. 我认为它与日志文件相比很笨重。 I find it difficult to see the "big picture" with it being stored in the database. 我发现很难看到存储在数据库中的“大图”。 I'll admit I'm a log file person, I like being able to open a text file and look through (regex) it instead of using sql to try and search for something. 我承认我是一个日志文件的人,我喜欢能够打开文本文件并查看(正则表达式)而不是使用sql来尝试搜索某些内容。

The last place I worked we had log files of 100 meg plus. 我工作的最后一个地方有100兆加的日志文件。 They're a little difficult to open, but if you have the right tool it's not that bad. 它们有点难以打开,但如果你有合适的工具,它就不那么糟糕了。 We had a system to log messages too. 我们有一个系统来记录消息。 You could quickly look at the file and determine which set of log entries belonged which process. 您可以快速查看该文件并确定哪个日志条目属于哪个进程。

We've used SQL Server centralized logging before, and as the previous posted mentioned, the biggest problem was that interrupted connectivity to the database would mean interrupted logging. 我们之前使用过SQL Server集中式日志记录,并且正如前面提到的那样,最大的问题是与数据库的中断连接意味着中断日志记录。 I actually ended up adding a queuing routine to the logging that would try the DB first, and write to a physical file if it failed. 实际上我最终在日志中添加了一个排队例程,它将首先尝试数据库,如果失败则写入物理文件。 You'd just have to add code to that routine that, on a successful log to the db, would check to see if any other entries are queued locally, and write those too. 您只需要在该例程中添加代码,在成功登录到数据库的日志中,将检查是否有任何其他条目在本地排队,并编写这些代码。

I like having everything in a DB, as opposed to physical log files, but just because I like parsing it with reports I've written. 我喜欢在数据库中拥有所有内容,而不是物理日志文件,但仅仅因为我喜欢使用我编写的报告来解析它。

I think the problem you have with logging could be solved with logging to SQL, provided that you are able to split out the fields you are interested in, into different columns. 我认为您可以通过记录到SQL来解决日志记录中遇到的问题, 前提是您可以将您感兴趣的字段拆分到不同的列中。 You can't treat the SQL database like a text field and expect it to be better, it won't. 您不能像处理文本字段那样对待SQL数据库并期望它更好,但事实并非如此。

Once you get everything you're interested in logging to the columns you want it in, it's much easier to track the sequential actions of something by being able to isolate it by column. 一旦获得了您想要记录到所需列的所有内容,通过能够按列隔离某些内容,可以更轻松地跟踪某些内容的顺序操作。 Like if you had an "entry" process, you log everything normally with the text "entry process" being put into the "logtype" column or "process" column. 就像你有一个“进入”过程一样,你可以正常记录所有内容,并将文本“entry process”放入“logtype”列或“process”列。 Then when you have problems with the "entry process", a WHERE statement on that column isolates all entry processes. 然后,当您对“入口过程”有问题时,该列上的WHERE语句将隔离所有入口进程。

we do it in our organization in large volumes with SQL Server. 我们在我们的组织中使用SQL Server进行大批量的操作。 In my openion writing to database is better because of the search and filter capability. 在我的openion中,由于搜索和过滤功能,写入数据库更好。 Performance wise 10 to 50 MB worth of data and keeping it only for 5 days, does not affect your application. 性能明智的10到50 MB的数据并且仅保留5天,不会影响您的应用程序。 Tracking transaction and users will be very easy compare to tracking it from text file since you can filter by transaction or user. 跟踪事务和用户与从文本文件中跟踪它相比非常容易,因为您可以按事务或用户进行过滤。

You are mentioning that the files read rarely. 你提到很少读取文件。 So, decide if is it worth putting time in development effort to develop the logging framework? 那么,决定是否值得花时间开发日志框架来开发日志框架? Calculate your time spent on searching the logs from log files in a year vs the time it will take to code and test. 计算一年中从日志文件中搜索日志所花费的时间与编码和测试所需的时间。 If the time spending is 1 hour or more a day to search logs it is better to dump logs in to database. 如果每天花费1小时或更长时间来搜索日志,最好将日志转储到数据库中。 Which can drastically reduce time spend on solving issues. 这可以大大减少解决问题的时间。

If you spend less than an hour then you can use some text search tools like "SRSearch", which is a great tool that I used, searches from multiple files in a folder and gives you the results in small snippts ("like google search result"), where you click to open the file with the result interested. 如果您花费不到一个小时,那么您可以使用一些文本搜索工具,如“SRSearch”,这是我使用的一个很棒的工具,从文件夹中的多个文件搜索,并以小的snippts(“像谷歌搜索结果”)给你结果“),您单击以打开感兴趣的结果的文件。 There are other Text search tools available too. 还有其他文本搜索工具可用。 If the environment is windows, then you have Microsoft LogParser also a good tool available for free where you can query your file like a database if the file is written in a specific format. 如果环境是Windows,那么Microsoft LogParser也是一个免费的好工具,如果文件是以特定格式编写的,您可以像数据库一样查询文件。

Here are some additional pros and cons and the reason why I prefer log files instead of databases: 以下是一些额外的优点和缺点以及我更喜欢日志文件而不是数据库的原因:

  1. Space is not that cheap when using VPS's. 使用VPS时,空间并不便宜。 Recovering space on live database systems is often a huge hassle and you might have to shut down services while recovering space. 在实时数据库系统上恢复空间通常是一个巨大的麻烦,您可能必须在恢复空间时关闭服务。 If your logs is so important that you have to keep them for years (like we do) then this is a real problem. 如果您的日志非常重要,您必须保留它们多年(就像我们一样),那么这是一个真正的问题。 Remember that most databases does not recover space when you delete data as it simply re-uses the space - not much help if you are actually running out of space. 请记住,大多数数据库在删除数据时都不会恢复空间,因为它只是重新使用空间 - 如果实际上空间不足,则没有多大帮助。

  2. If you access the logs fequently and you have to pull daily reports from a database with one huge log table and millions and millions of records then you will impact the performance of your database services while querying the data from the database. 如果您经常访问日志,并且必须从具有一个巨大日志表和数百万条记录的数据库中提取每日报告,那么在查询数据库中的数据时,您将影响数据库服务的性能。

  3. Log files can be created and older logs archived daily. 可以创建日志文件,并且每天存档旧日志。 Depending on the type of logs massive amounts of space can be recovered by archiving logs. 根据日志类型,可以通过归档日志来恢复大量空间。 We save around 6x the space when we compress our logs and in most cases you'll probably save much more. 当我们压缩日志时,我们节省了大约6倍的空间,在大多数情况下,您可能会节省更多。

  4. Individual smaller log files can be compressed and transferred easily without impacting the server. 可以轻松压缩和传输单个较小的日志文件,而不会影响服务器。 Previously we had logs ranging in the 100's of GB's worth of data in a database. 以前我们在数据库中有100英镑GB的数据。 Moving such large databases between servers becomes a major hassle, especially due to the fact that you have to shut down the database server while doing so. 在服务器之间移动这样大的数据库成为一个主要的麻烦,特别是由于您必须在这样做时关闭数据库服务器。 What I'm saying is that maintenance becomes a real pain the day you have to start moving large databases around. 我所说的是,在您必须开始移动大型数据库的那一天,维护成为一种真正的痛苦。

  5. Writing to log files in general are a lot faster than writing to DB. 一般写入日志文件比写入数据库要快得多。 Don't underestimate the speed of your operating system file IO. 不要低估操作系统文件IO的速度。

  6. Log files only suck if you don't structure your logs properly. 如果您没有正确构建日志,日志文件只会很糟糕。 You may have to use additional tools and you may even have to develop your own to help process them, but in the end it will be worth it. 您可能必须使用其他工具,甚至可能需要开发自己的工具来帮助处理它们,但最终它将是值得的。

I've been reading all the answers and they're great. 我一直在阅读所有的答案,他们很棒。 But in a company I worked due to several restrictions and audit it was mandatory to log into a database. 但在我工作的公司中,由于有几个限制和审计,必须登录数据库。 Anyway, we had several ways to log and the solution was to install a pipeline where our programmers could connect to the pipeline and log into database, file, console, or even forwarding log to a port to be consumed by another applications. 无论如何,我们有几种方法来记录,解决方案是安装一个管道,我们的程序员可以连接到管道并登录数据库,文件,控制台,甚至将日志转发到另一个应用程序使用的端口。 This pipeline doesn't interrupt the normal process and keeping a log file at the same time you log into the database ensures you rarely lose a line. 此管道不会中断正常进程,并且在您登录数据库的同时保留日志文件可确保您很少丢失一行。 I suggest you investigate further log4net that it's great for this. 我建议你进一步研究log4net这对它来说很棒。

http://logging.apache.org/log4net/ http://logging.apache.org/log4net/

You could log to a comma or tab delimited text format, or enable your logs to be exported to a CSV format. 您可以登录逗号或制表符分隔的文本格式,或者将日志导出为CSV格式。 When you need to read from a log export your CSV file to a table on your SQL server then you can query with standard SQL statements. 当您需要从日志中读取CSV文件导出到SQL服务器上的表时,您可以使用标准SQL语句进行查询。 To automate the process you could use SQL Integration Services. 要自动执行该过程,您可以使用SQL Integration Services。

I could see it working well, provided you had the capability to filter what needs to be logged and when it needs to be logged. 我可以看到它的工作好,只要你有过滤什么需要记录, 需要记录它的能力。 A log file (or table, such as it is) is useless if you can't find what you're looking for or contains unnecessary information. 如果您找不到要查找的内容或包含不必要的信息,则日志文件(或表格,例如它)将毫无用处。

Since your logs are read rarely, I would write them on file (better performance and reliability). 由于您的日志很少被读取,我会将其写入文件(更好的性能和可靠性)。

Then, if and only if you need to read them, I would import the log file in a data base (better analysis). 然后,当且仅当您需要阅读它们时,我会在数据库中导入日志文件(更好的分析)。

Doing so, you get the advantages of both methods. 这样做,您可以获得两种方法的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM