.NET 的最佳翻转日志文件跟踪侦听器是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/156575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What the best rollover log file tracelistener for .NET
提问by user17222
I'm looking for a good TraceListener for .Net that supports rolling over the log file based on size limits.
我正在为 .Net 寻找一个好的 TraceListener,它支持根据大小限制滚动日志文件。
Constraints
约束
- Uses .Net built in Trace logging
- Independent class or binary that's not part of some gigantic library
- Allows rolling over a log file based on size
- 使用 .Net 内置的跟踪日志记录
- 不属于某个巨大库的独立类或二进制文件
- 允许根据大小滚动日志文件
回答by csgero
You could use Microsoft.VisualBasic.Logging.FileLogTraceListener, which comes built-in with the .NET Framework. Don't let the VisualBasic in the namespace scare you, you'll just have to reference the microsoft.visualbasic.dll assembly and it should work fine with C#.
您可以使用Microsoft.VisualBasic.Logging.FileLogTraceListener,它是 .NET Framework 内置的。不要让命名空间中的 VisualBasic 吓到您,您只需要引用 microsoft.visualbasic.dll 程序集,它应该可以在 C# 中正常工作。
回答by Ostati
I keep this config snippet handy for whenever I need to do the network trace. I don't have to have a project built with explicit reference to VB DLL added as this does by adding the reference in App.config at runtime.
每当我需要进行网络跟踪时,我都会把这个配置片段放在手边。我不必像这样通过在运行时在 App.config 中添加引用来构建一个显式引用 VB DLL 的项目。
<system.diagnostics>
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Http">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose"/>
<add name="System.Net.Http" value="Verbose"/>
<add name="System.Net.Sockets" value="Verbose"/>
</switches>
<sharedListeners>
<add name="System.Net"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="DateTime,ProcessId,ThreadId"
customLocation="c:\temp"
location="Custom"
logFileCreationSchedule="Daily"
baseFileName="NetworkTrace"/>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
And add the reference at runtime
并在运行时添加引用
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.VisualBasic" culture="neutral" publicKeyToken="b03f5f7f11d50a3a"/>
<codeBase version="10.0.0.0" href="file://C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/.NETFramework/v4.5/Microsoft.VisualBasic.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
回答by Aaron Powell
I'm a big fan of log4net (http://logging.apache.org/log4net/index.html), it's very easy to configure and supports just about any log type you want, but can have custom ones written as well.
我是 log4net ( http://logging.apache.org/log4net/index.html) 的忠实粉丝,它非常容易配置并支持您想要的任何日志类型,但也可以编写自定义日志类型。
It can also do different actions depending on the log level. We log all messages to a text file and Error -> Fatal send emails
它还可以根据日志级别执行不同的操作。我们将所有消息记录到一个文本文件中,然后错误 -> 致命发送电子邮件
回答by Robert Vukovi?
回答by RhysC
I have used both Log4Net and Nlog. I prefer NLog but really once they are setup you forget they are there anyway (untill something breaks, then your glad it is there!). there should be plenty of documentation on both out in the interweb
我使用过 Log4Net 和 Nlog。我更喜欢 NLog,但实际上一旦它们被设置,你就会忘记它们在那里(直到出现问题,然后你很高兴它在那里!)。互联网上应该有大量关于两者的文档
回答by Mark Sowul
FileLogTraceListener is a common suggestion but it throws away events or throws an exception when the file exceeds the given max size.
FileLogTraceListener 是一个常见的建议,但当文件超过给定的最大大小时,它会抛出事件或抛出异常。
We created a class that extends it, and overrides the Write/WriteLine methods.
我们创建了一个扩展它的类,并覆盖了 Write/WriteLine 方法。
There's a try/catch (InvalidOperationException), and if that happens, we call base.Closeand rename the file (FullLogFileName) as follows (we need the base.Closeor else we'll get a 'file in use' error):
有一个try/catch (InvalidOperationException), 如果发生这种情况,我们调用base.Close并重命名文件 ( FullLogFileName) 如下(我们需要 ,base.Close否则我们将收到“文件正在使用”错误):
In a loop, we add a number to the end, and see if that file exists; if not, use File.Move(FullLogFileName, newFileWithNumber), otherwise we keep incrementing the number until we find a file name that works. There's also a lock to ensure that the given instance is thread safe.
在循环中,我们在末尾添加一个数字,然后查看该文件是否存在;如果没有,请使用File.Move(FullLogFileName, newFileWithNumber),否则我们会不断增加数字,直到找到一个有效的文件名。还有一个锁来确保给定的实例是线程安全的。
回答by Grigori Melnik
Consider Enterprise Library Logging Application Block
Make sure to install only the Logging block, since EntLib installer checks all blocks by default.
确保只安装 Logging 块,因为默认情况下 EntLib 安装程序会检查所有块。
回答by Chris S
As given in one of the comments:
正如评论之一所给出的:
FileLogTraceListener Class
FileLogTraceListener 类
Writes to a rolling text file.
写入滚动文本文件。
Remarks
A new file is used when the maxFileSize is reached, as well as a daily or weekly basis as specified by logFileCreationSchedule.
Each file has a name in the format "\(-)(-).log", with the local date included for daily and weekly rotation, and a sequence number appended if the file already exists.
评论
当达到 maxFileSize 以及 logFileCreationSchedule 指定的每天或每周时,将使用一个新文件。
每个文件都有一个格式为“\(-)(-).log”的名称,其中包含每日和每周轮换的本地日期,如果文件已经存在,则附加一个序列号。

