Register using the backup application

Is there any .NET logging platform that has the ability to switch the application if something is wrong with the current one. In fact, I want to:

If I use the database application and when something goes wrong with the database (for example, the server goes down, lose power ...) I want to switch to the second file (for example, which is included in the file).

Does this ability have one of the following: log4net, NLog, corporate library? I searched for it, but no luck.

+4
source share
5 answers

Since log4netContribute FallbackAppender does not work correctly, I did some deep research and found that this ability has nLog. I tested it and it works like a charm;) Here is an example:

[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"); 
+1
source

As far as I know that log4net does not currently support application backups, there is (or was?) An open problem in the log4net function lagging. But I think a project called FallbackAppender is doing exactly what you need.

+2
source

For completeness: the corporate library supports a custom custom error source in which you can install an "appender" to log error messages. Once set up, it just works without programming.

The only drawback is that it will actually register an exception that occurred along with the details of the log entry in a specific format that cannot be changed, so it is not flexible. This is useful for troubleshooting, but it may not be ideal if you want to extract the error log messages and import them into the original registration destination (although the format is known, so it could be analyzed).

+2
source

Yes, Log4Net allows you to have multiple log destinations, such as a log file, email, database, and event viewer.

You can change the destination in the application configuration file. You can also run more than one at a time - for example, log into the event viewer and database.

I always recommend having two default destinations for the journal - in case one of them has problems.

+1
source

NLog allows you to log in for several purposes through a configuration file. Remember to set ignoreFailures to true to ensure that any install / uninstall errors are ignored:

 <?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 the database target in the NLog documentation for more information.

Edit: You can also create a custom goal 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 details.

+1
source

Source: https://habr.com/ru/post/1390809/


All Articles