Nlog Lut

0 views
Skip to first unread message

Annette Fazzari

unread,
Aug 4, 2024, 5:01:55 PM8/4/24
to salirixy
Ive been trying to get an ASP.NET Core site working with NLog. It works fine until I try to write to a SQL Database. I've tried local databases and Azure databases - all with the same problem. I've even added the nlog table to a known database, one the site already connects to (with EF).

Error Error when writing to database. Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.


I had done some reading around and looks NLog is the best option being simple and most features I need.My plan is to write a wrapper class library around NLog and use my library in all my asp.net applications, web api as well as other library projects and some classic asp sites using COM+.


NLog and others (I highly recommend Serilog) are used for runtime diagnostics and tracing... That is, to dump information which you are either tracing, or logging (which is, by definition, not uncaught exceptions).


NLog, as like log4net, nlog is getting quite long in the tooth and while there was a recent release... the previous release a year earlier, and I feel most of the "old guard" logging frameworks have not really grown with the times.


When it comes to finding a bug in an app, few things beat a good trail of logs. And for writing those logs, few tools beat NLog in simplicity or flexibility. I rarely appreciate just how flexible it is though, so it's worth spending a little time taking a closer look.


Generally when I've used NLog, I configure it the same ol' way.. writing logs to a file. To use NLog's vernacular, I target a file.. but there are a lot of other possible targets too. In fact there's nearly a hundred as I write this.


I've written about NLog in WinForms before, and I think that's still a decent enough tutorial for getting started, so I won't rehash it here. The NLog readme and wiki has a ton of resources as well, like this one for getting started on the .NET Framework.


If you've got everything configured but nothing's happening, double-check that the NLog.config file is set to copy to the output directory. If it's not in the bin folder when the app runs, nothing'll happen. Otherwise, follow their troubleshooting doc.


The first two are from the NLog team, and the third one is from someone else (Dariusz Danielewski). It's awesome that anyone can create their own targets, but it's also prudent to give any code from a third-party a cursory review at least. NLog has it listed on their site, so I'd hope they at least vetted it out initially, but anything can change over time.


In general, all the targets are good about including a short section that shows how to modify your nlog.config file to get things running, and the above three are no exception. Here's what my config file looks like after adding them.


Better yet, if you decide to add more targets later, you can do it with minimal (or no) changes to your code. Below is part of my Form.. since I didn't give the various loggers names, and I don't specify one in the constructor below, they all apply all the time, filtered only by the "minlevel" attributes in the config file.


Here's a short demo showing it in action. There are buttons in the Form to send messages of all varying levels, from trace to fatal. The textbox is just to simulate the informational message an app might show to the user, or a friendlier message that might get attached to an exception.


@Anonymous thank you, I have the same problem - nlog don't create folder and log files. I noticed LogManager.Configuration was Null and try you method. But it not helps me, log files dont creates whatever. Do you know what could be the problem?


NLog is a logging framework for .NET. It has rich log routing and managementcapabilities and greatly helps you to produce and manage logs. NLog supportsstructured logs, multiple logging targets, and everything a modern loggingframework should support. In the long list of the NLog's features, you can find:


Before starting to work on the application, you need to install some dependencypackages. Visual Studio provides multiple ways to use the NuGet Package Manager.In the tutorial we will use the Package Manager Console.


The first thing you have to do is to open the Package Manager Console. You cando it using Tools > NuGet Package Manager > Package Manager Console.Alternatively, you can sequentially press ALT, V, E, and O.


NLog has rich configuration abilities and provides two ways to configure thelogger: programmatically or via a configuration file. You can get advancedinformation about each of themin the documentation.


Then, you have to set the file's Copy to Output Directory property to Copyalways. It can be done by right-clicking the file in the solution explorer andselecting the Properties option.


We've decided to add 3 logging targets for the application: the colorizedconsole and 2 files. In the config file, each of them will be represented by aseparate XML element. Let's write the targets in the nlog.config file.


Proper logging can greatly assist in the support and development of yourapplication. This may seem like a daunting task, but NLog is a fast andconfigurable logging framework that greatly simplifies the task.


The easiest way to add the NLog and the InsightOps Target libraries to your application is to install the logentries.nlog Nuget package. This package will install the InsightOps Target library and will also automatically install the NLog package as a dependency.


NLog allows log messages to be sent to multiple destinations. In NLog terminology, such an output destination is called a target. Targets must subclass the NLog.Targets.Target class. The InsightOps Plugin library provides such a target component that is specifically designed to send log messages to InsightOps in an efficient manner.


Token: The unique token GUID of the log to send messages to. This applies when using the newer token-based logging.AccountKey and Location: The account key and location to send messages to. This applies when using the older HTTP PUT logging (see below for more information).Unlike the logging settings (which are typically configured once for a given application) the InsightOps credentials typically vary based on the environment or instance of your application. For example, your application might run in both a testing and a production environment, and you will most likely wish to have separate logging destinations for those two environments.


If the value exists as a Windows Azure role configuration setting, that value is used.Otherwise if the value exists as a setting in the element in your App.config or Web.config file, that value is used.Otherwise if the value exists as a configured child element of the element, that value is used.Here is an example of how to specify the credentials in the element:


Our recommended method of sending messages to InsightOps is via Token TCP over port 10000. To use this method, select Token TCP as the source type when creating a new log in the InsightOps UI, and then paste the token that is printed beside the log in the value for the Logentries.Token credential setting.


The InsightOps appender supports sending log data over SSL/TLS with both of the above logging methods by setting the useSsl logging setting to true in the appender definition. This is more secure but may have a performance impact.


private static readonly Logger m_logger = LogManager.getCurrentClassLogger();This creates a logger with the same name as the current class, which organizes the NLog configuration hierarchy according to your code namespace hierarchy. This provides both clarity when reading the logs, and convenience when configuring different log levels for different areas of your code.


The InsightOps target keeps an internal queue of log messages and communicates with the InsightOps system using a background thread which continuously sends messages from this queue. Because of this, when an application is shutting down, it is possible that some log messages might still remain in the queue and will not have time to be sent to InsightOps before the application domain is shut down.


To work around this potential problem, consider adding the following code to your application, which will block for a moment to allow the InsightOps appender to finish logging all messages in the queue. The AreAllQueuesEmpty() blocks for a specified time and then returns true or false depending on whether the queues had time to become empty before the method returns.


Once upon a time, when there were no debuggers in the world and software was mostly console-based, programmers used to output tracing messages using printf() statements. Today's world has seen huge advancement in technology, and printf() has been replaced with Console.WriteLine().


Console.WriteLine() statements in the above example are called "tracing statements" because they are only used to report our application's control flow, and have no other function. The output of Console.WriteLine() is called the program trace. In this example, the tracing statements produce output that tells us if the DoSomething() method has finished execution or not.


After giving the application some testing, we tend to remove the tracing code in order to improve performance (tracing can take a lot of time). Tracing instructions are usually commented out so that they can be re-enabled in the future. Unfortunately, this requires our program to be recompiled.


It may seem that in the age of graphical debuggers, the usefulness of tracing-based solutions is limited. Sometimes, tracing turns out to be the only tool available, which can be used to locate bug in a mission-critical system that cannot be switched off for a single minute.


NLog is an open source library distributed at no cost under the terms of the BSD license, which permits commercial usage with almost no obligation. You can download the NLog binary and source code releases from its website. A graphical installer is also provided, which lets you install NLog in a preferred place, and enables integration with Visual Studio 2005 (Express editions are also supported), including:

3a8082e126
Reply all
Reply to author
Forward
0 new messages