简体   繁体   English

使用备份appender进行日志记录

[英]Logging with backup appender

Is there any .NET logging framework that have ability to switch appender if something is wrong with current one. 是否有任何.NET日志框架能够在当前问题出现问题时切换appender。 Actually what I want is following: 其实我想要的是:

If I'm using database appender and when something goes wrong with database (eg server goes down, lose power...) I want to switch to second appender (eg which log into file). 如果我正在使用数据库appender并且当数据库出现问题时(例如服务器停机,失去电源......)我想切换到第二个appender(例如,哪个登录到文件)。

Does this ability have one of following: log4net, NLog, Enterprise Library? 此功能是否具有以下功能之一:log4net,NLog,Enterprise Library? I was looking for this but no luck. 我在寻找这个,但没有运气。

For completeness: Enterprise Library supports a configurable Error Special Source where you can set an "appender" to log messages that have errored. 为了完整性:Enterprise Library支持可配置的错误特殊源,您可以在其中设置“appender”以记录出错的消息。 Once configured this just works without any programming. 配置完成后,无需任何编程即可正常工作

The only downside is that it will actually log the exception that occurred along with the Log Entry details in a specific format that cannot be changed so it is not flexible. 唯一的缺点是,它实际上会以特定格式记录发生的异常以及日志条目详细信息,这些格式无法更改,因此不灵活。 This is good for troubleshooting but it might not be ideal if you want to extract errored log messages and import them into the original logging destination (although the format is known so it would be possible to parse). 这对于故障排除很有用,但如果要提取错误的日志消息并将其导入原始日志记录目标(尽管格式已知,因此可以解析),这可能并不理想。

As far as I know log4net currently does not support backup appenders, there is (or was?) an open issue in the log4net feature backlog. 据我所知,log4net目前不支持备份appender,log4net功能积压中存在(或者是?)一个未解决的问题。 But I think that the project called FallbackAppender does exactly what you need. 但我认为名为FallbackAppender的项目完全符合您的需求。

Because log4netContribute FallbackAppender is not working as expected I've made deep research and found that this ability has nLog. 因为log4netContribute FallbackAppender没有按预期工作,所以我做了深入的研究,发现这个能力有nLog。 I tested it and it is working like a charm ;) Here is an example: 我测试了它,它的工作就像一个魅力;)这是一个例子:

[app.config] [的app.config]

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="appTitle" value="My Application"/>
<targets>
<target name="file" xsi:type="FallbackGroup" returnToFirstOnSuccess="true">
<target xsi:type="File" fileName="x:\vladimir.txt" />
<target xsi:type="File" fileName="w:\pavlovic.txt" />
<target xsi:type="File" fileName="w:\zvjerka24.txt" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>

[code] [码]

Logger _logger = LogManager.GetCurrentClassLogger();
_logger.Info("Neki Info");
_logger.Debug("Neki debug");
_logger.Error("Neki  ERROR");
_logger.Error("Pa jos neki");

Yes, Log4Net allows you to have multiple log destinations, such as: Log file, Email, Database, and Event Viewer. 是的, Log4Net允许您拥有多个日志目标,例如:日志文件,电子邮件,数据库和事件查看器。

You can change the destination in the application's config file. 您可以在应用程序的配置文件中更改目标。 You can also run more than one at the same time - eg log to the Event Viewer and database. 您还可以同时运行多个 - 例如,登录事件查看器和数据库。

I'd always recommend having two log destinations by default - in case one of the them has a problem. 我总是建议默认情况下有两个日志目的地 - 以防其中一个出现问题。

NLog allows you to log to multiple targets via a configuration file. NLog允许您通过配置文件登录多个目标。 Don't forget to set ignoreFailures to true to ensure that any install/uninstall failures are ignored: 不要忘记将ignoreFailures设置为true以确保忽略任何安装/卸载失败:

<?xml version="1.0" encoding="utf-8" ?> 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="logfile" xsi:type="File" />
        <target name="db" xsi:type="Database" />
    </targets>

    <rules>
        <logger name="*" levels="Info" writeTo="logfile,db" />
    </rules>
</nlog> 

See database target in the NLog documentation for more information 有关详细信息,请参阅NLog文档中的数据库目标

Edit: You could also create a custom target in order to achieve this in code: 编辑:您还可以创建自定义目标,以便在代码中实现此目标:

using NLog; 
using NLog.Targets; 

namespace MyNamespace 
{ 
    [Target("MyFirst")] 
    public sealed class MyFirstTarget: TargetWithLayout 
    { 
        public MyFirstTarget()
        {
            this.Host = "localhost";
        }

        [Required] 
        public string Host { get; set; }

        protected override void Write(LogEventInfo logEvent) 
        { 
            string logMessage = this.Layout.Render(logEvent); 

            SendTheMessageToRemoteHost(this.Host, logMessage); 
        } 

        private void SendTheMessageToRemoteHost(string host, string message) 
        { 
            // Detect your database state here or do something else with the error.
        } 
    } 
}

and use it with: 并使用它:

<nlog> 
  <extensions> 
    <add assembly="MyAssembly"/> 
  </extensions> 
  <targets> 
    <target name="a1" type="MyFirst" host="localhost"/> 
  </targets> 
  <rules> 
    <logger name="*" minLevel="Info" appendTo="a1"/> 
  </rules> 
</nlog>

See this page for more information. 有关更多信息,请参阅此页面

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

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