使用 NLog 集中日志记录的最佳方式是什么?
我被分配到一个项目,其中有很多基于 SharePoint 的编写质量很差的代码。
它由大约 15 个子项目组成,其中一些是 Windows 服务、一些 Web 服务、一些在 SharePoint 内部运行的 Web 应用程序、一些是 Web 部件甚至控制台应用程序。它们都在同一台服务器上运行并互相调用。
生产中已经存在很多问题,但很难追踪。
从他不懈地努力捕捉所有例外情况来看,最初的开发者一定是《塞林格》或《神奇宝贝》系列的粉丝。不幸的是,这些事件都没有被报告或记录。
我当前的任务是将日志记录引入整个项目,以便我可以找到现在不可见的异常,跟踪纠结的重复调用,并至少有一些堆栈跟踪。我决定使用 NLog,因为它活跃且很酷,而不是 log4net 非常好,但有点不合我的口味。
由于组件紧密耦合,因此我希望将日志记录集中在一个文件中,这样相关错误就不会分散在硬盘驱动器中。因此,我希望有两个或三个不同的日志文件,其中五个或更多项目或多或少同时写入每个日志文件。
配置 NLog 来集中日志记录的最佳方法是什么? 我应该为每个项目都有一个配置文件,还是相关项目应该共享它们?我应该将配置文件放在哪里以从 SharePoint Web 部件进行日志记录?我会遇到任何许可问题吗?
我正在使用 SharePoint 2007。
I have been assigned a project with a lot of poorly written code that is based around SharePoint.
It consists of about 15 subprojects, some of them being windows services, some web services, some web applications running inside of SharePoint, some being webparts and even console applications. They all run on the same server and call each other.
There are already many issues in production but they are hard to trace down.
The original developer must have been a fan of either Salinger or Pokémon series judging by his tireless effort to catch all exceptions. Unfortunately, none of them get reported or logged, ever.
My current task is to introduce logging into the whole project so I could find now-invisible exceptions, follow tangled recurring calls and have some stack traces at least. I decided to go with NLog, seeing it's active and cool, as opposed to log4net which is perfectly fine but somewhat not as fancy to my taste.
Because the components are tightly coupled, I want to centralize logging in one file so related errors don't get scattered across the hard drive. Therefore, I am looking to have two or three different log files with five or more projects writing to each of them more or less simultaneously.
What is the best way to configure NLog to centralize logging? Should I have a config file for each project, or should related projects share them? Where should I put config file to log from SharePoint webparts? Am I going to face any permission issues?
I'm using SharePoint 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的集中化方法可能就是简单地记录到数据库,其中一个好处是多个应用程序写入数据库比写入同一日志文件更容易。对于每个应用程序,配置 NLog 以记录到数据库目标,并为每个应用程序使用相同的数据库目标配置参数。您的 NLog.config 文件可能看起来像这样:
除了(或代替)记录到数据库之外,您当然还可以记录到文件。
我不熟悉从 SharePoint 执行此操作,因此我无法评论您可能遇到的任何配置或权限问题。
以下是我找到的一个链接,其中讨论了如何让 NLog 在 SharePoint 环境中工作:
http://nlog-forum.1685105.n2.nabble.com/Is-anyone-using-NLog-with-SharePoint-td2171451.html
链接似乎会将您置于 NLog 论坛的顶部,而不是特定的帖子。在论坛“有人使用 NLog 与 SharePoint”中搜索此文本,您应该会找到正确的帖子。
祝你好运!
The easiest way to centralize is probably to simply log to a database, one benefit being that multiple applications and write to the database easier than to the same log file. For each application, configure NLog to log to the Database target, using the same Database target configuration parameters for each. Your NLog.config file might look something like this:
You could certainly log to files in addition to (or instead of) logging to a database.
I am not familiar with doing this from SharePoint, so I can't comment on any configuration or permission issues that you might run into there.
Here is a link I found where there is a discussion of getting NLog to work in a SharePoint environment:
http://nlog-forum.1685105.n2.nabble.com/Is-anyone-using-NLog-with-SharePoint-td2171451.html
That link appears to put you at the top of the NLog forum instead of the specific post. Search for this text in the forum "Is anyone using NLog with SharePoint" and you should find the right post.
Good luck!
您还可以利用 SharePoint 中现有的日志记录基础结构并写入 ULS 日志。这样,您就可以使用 ULS 日志查看器 在完整的上下文中查看日志信息。对于 SharePoint 2007,请参阅此博客如何写入 ULS 日志:
SharePoint 跟踪日志和统一日志记录服务 (ULS)
在 SharePoint 2010 中,通过对 SPDiagnosticsService 类,您可以在其中使用新的 WriteTrace 方法。
You could also just leverage the existing logging infrastructure in SharePoint and write to the ULS logs. This way your log information can be viewed in a complete context using the ULS log viewer. For SharePoint 2007 see this blog how to write to the ULS log:
SharePoint Trace Logs and the Unified Logging Service (ULS)
With SharePoint 2010 it has become even easier with improvements to the SPDiagnosticsService class where you can use the new WriteTrace method.
我个人将异常记录到事件记录器。我使用 NLog 来记录详细信息、调试信息或跟踪。
由于 NLog 可以轻松打开和关闭,因此我仅在调试或需要检查生产中的异常时才激活它。我从来都不是 .NET 中默认跟踪功能的忠实粉丝。
我更喜欢简单的纯文本日志文件。尽管如果您的代码中没有实现太多“日志行”,那么记录到数据库会非常有效。
Personally I log Exceptions to the Event Logger. And I use NLog for logging details, debug information or tracing.
Since NLog can be easily switched on and off I only activate it when I'm debugging or when I need to inspect an exception in production. I never was a big fan of the default tracing functionality in .NET.
I prefer simple plain text log files. Although logging to a database works great if you don't have too many "log lines" implemented in your code.
我感觉我们在做同一个项目!由 Web 项目、核心 dll 项目、控制台应用程序、服务等组成的多个项目。不幸的是,我不像您那样在 sharepoint 中工作,但我可以描述我如何尝试集中我们的日志记录。
我们有 1 个核心 .Net 框架项目。这是我放置日志包装类的地方。该项目还包含 nlog dll 和 nlog 配置文件。在此核心项目文件中,您可以添加此文件,当您构建依赖于此核心项目的项目时,它会自动移动配置。
我们发现一些不编译 dll 的 Web 项目不会自动提取配置文件,因此我们将其留给构建过程。这有助于集中我们的日志记录,因此您只需管理所有配置的单个配置。
此外,请记住,当您为每个类创建记录器时,日志名称中应包含命名空间,因此,如果您希望为特定项目进行不同的设置,则可以创建基于命名空间进行过滤的特定目标。
至于集中日志的结束位置,我们选择使用文件目标并指定完整路径。这是因为在我们的服务器上,应用程序在 C:\ 上运行,但我们有更大的 D:\ 可以存储日志。在我们的生产服务器中,我们也有多个服务器,因此我们使用 splunk 来聚合所有日志。
如果 splunk 是不可能的,并且您使用的是分布式系统,那么数据库听起来是一个好主意,如上所述。如果您不想建立 sql 实例,也有 mongo db 的目标包装器。
希望有帮助,我很好奇是否有人对我的做法有建议或意见!
I feel like we are working on the same project! Multiple projects consisting of web projects, core dll projects, console apps, services, etc. Unfortunately I'm not working in sharepoint like you are, but I can describe how I am trying to centralize our logging.
We have 1 core .Net framework project.This is where I placed our wrapper class of the log. This project also holds the nlog dlls and the nlog config file. In this core project file you can add this which automatically moves the config when you build projects with a dependency on this core project.
We found that some web projects that don't compile a dll don't automatically pull in the config file, so we will leave that up to the build process. This helps centralize our logging so you only have to manage a single config across them all.
In addition remember when you create a logger per class, the log name should have the namespace in it, so you can make specific targets that filter based off namespace if you want different settings for particular projects.
As for centralizing where the logs end up, we chose to use a file target and specify the full path. This is because on our servers the applications run off a C:\ but we have larger D:\ which can store the logs. In our production servers, we also have multiple servers, so we are using splunk to aggregate all of our logs.
If splunk is out of the question and you are on a distributed system, a database sounds like a good idea as suggested above. If you don't want to stand up an sql instance, there are target wrappers for mongo db as well.
Hopefully helpful, I'm curious if anyone has suggestions or opinions on how I'm doing it as well!