数据库日志记录什么?
我对 SQL Server 很陌生,想知道 SQL Server 日志和自定义日志(在我的例子中,使用 log4net)之间有什么区别?我想使用 log4net
记录的内容有更多选择,但是数据库会自动记录哪些内容?例如,如果用户注册到我的网站,我是否必须手动记录该事务,或者是否会自动记录在数据库日志中?我目前正在启动一个项目,想弄清楚我应该记录什么。
谢谢
I'm quite new to SQL Server and was wondering what the difference between the SQL Server log is and a custom log (in my case, using log4net)? I guess there's more choice on what to log using log4net
, but what things are automatically logged by the database? For example, if a user signs up to my site, would I have to manually log that transaction, or would that be recorded in the database's log automatically? I'm currently starting a project and would like to figure out exactly what I should bother logging.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
苹果和橙子。
Log4net 和其他自定义“日志记录”只是捕获应用程序报告的事件的一种方法。在这种情况下,“日志”指的是该基础设施使用的任何存储来保存有关这些事件的信息。
另一方面,数据库日志则完全不同。为了保持一致性和原子性,数据库使用所谓的 Write-Ahead-Log 协议。在 WAL 中,所有更改在应用于数据之前首先会持久写入日志或日志中。这允许恢复重播日志(日志)并通过回滚任何未提交的工作使数据恢复到一致状态。
数据库日志与您的应用程序代码完全无关。任何数据库更新都将由引擎自动记录,因为这就是数据库中任何数据的更新方式。您无法修改它,也无法访问日志中写入的内容(严格来说,您可以查看日志,但您不会找到任何对您的应用程序有用的信息)。
Apples and Oranges.
Log4net and other custom 'logging' is just a way to capture events an application is reporting. 'Log' in this context reffers to whatever store is used by this infrastucture to persist information about these events.
The database log on the other hand is something compeltely different. In order to maintain consistency and atomicity databases use a so called Write-Ahead-Log protocol. In WAL all changes are first durable written into a journal, or log, before being applied to the data. This allows recovery to replay the log (the journal) and get the data back into a consistent state, by rolling back any uncommited work.
Database logs have absolutely nothing to do with your application code. Any database update will be automatically logged by the engine, simply because this is how any data is updated in a database. You cannot modify that, nor do you have any access to what's written in the log (strictly speaking you can look into the log, but you won't find any usefull information for your application).
SQL 日志处理用于回滚或提交数据的事务日志记录。它们通常只由知道自己正在做什么、恢复备份或传送日志以用于备份的人来处理。
log4net 和其他日志框架在代码日志中处理您想要输出的您自己的信息的异常、警告或调试级别信息。它们可以发送到数据库、命令窗口、平面文件或 Web 服务中的表。常见的日志记录场景是在应用程序级别捕获未处理的异常以帮助跟踪错误,或者在写出堆栈跟踪的任何 try catch 语句中捕获未处理的异常。
SQL log handles tansaction logging for rolling back or comiting data. They are usually only dealt with by someone who knows what they are doing restoring backups or shipping the logs to use for backups.
The log4net and other logging framweworks handle in code logging of exceptions, warning, or debug level info that you would like to output for your own info. They can be sent to a table in a database, command window, flat file or web service. Common logging scenarios are catching unhandled exceptions at the application level to help track down bugs, or in any try catch statements writing out the stack trace.
它会跟踪事务,以便在发生崩溃时可以回滚或重放它们。比简单的日志记录要复杂得多。
It keeps track of the transactions so it can roll them back or replay in case of a crash. Quite more involved than simple logging.
两者几乎完全无关。
数据库日志用于回滚事务、从崩溃中恢复等。这些都是确保数据库一致性的好东西。它包含更新/插入/删除——实际上与意图或应用程序试图执行的操作无关,除非它直接影响数据库中的数据。
另一方面,应用程序日志(使用 Log4Net)在构建和调试应用程序时非常有用。它由您驱动,并且应包含跟踪您的应用正在执行的操作的信息。当您不再需要它时,可以安全地关闭或减少它(通过切换日志级别)。
The two are almost completely unrelated.
A database log is used to rollback transactions, recover from crashes, etc. All good things to ensure database consistency. It has updates/inserts/deletes in it--not really anything about intent or what your app is trying to do unless it directly affects data in the database.
The application log on the other hand (with Log4Net) can be extremely useful when building and debugging your application. It is driven by you and should contain information that traces what your app is doing. This is something that can safely be turned off or reduced (by toggling the log level) when you no longer need it.
SQL Server日志文件实际上是用来维护其自身的稳定性的,但对于普通开发人员来说并不是很有用。它不是你想的那样(我也不是我想的那样),是已经运行过的 SQL 语句的列表。它是一种专有格式,旨在帮助 SQL 从崩溃或回滚事务中恢复。
如果您需要跟踪系统中发生的情况,SQL 事务日志不会有帮助,并且很难取回该信息。相反,我建议在表上添加触发器,将信息写入另一个表,或者在数据层中添加一些代码来保存正在发生的情况的日志。它可以像用您自己的实现包装 SQL 命令对象一样简单,除了它正在执行的任何正常代码之外,它还可以将 SQL 语句保存到 log4net 中。
The SQL Server log file is actually used for maintaining it's own stability, but it's not terribly useful for normal developers. It's not what you think (and I what I thought), a list of SQL statements that have been run. It's a propriety format designed to help SQL recover from a crash or roll back transactions.
If you need to track what's going on in the system, the SQL transaction log won't be helpful, and it would be very difficult to get that information back out. Instead, I would suggest adding triggers on your tables that write information off to another table, or add some code in your data layer that saves off a log of what's going on. It could be as simple as wrapping the SQL command object with your own implementation, which saved SQL statements off to log4net in addition to whatever normal code it was executing.
这是 RMDBS 可以确保原子性和一致性,请参阅 酸性。
It is the mechanism by which the RMDBS can assure atomicity and consistency, see ACID.