using System; using NLog; class Program { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { Logger.Info("This is a test message for file logging."); } }
using System; using NLog; class Program { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { var config = new NLog.Config.LoggingConfiguration(); var consoleTarget = new NLog.Targets.ConsoleTarget("console"); config.AddRule(LogLevel.Info, LogLevel.Fatal, consoleTarget); LogManager.Configuration = config; Logger.Debug("This is a debug message for console logging."); } }
using System; using NLog; using NLog.Targets; using NLog.Config; class Program { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); static void Main(string[] args) { var config = new LoggingConfiguration(); // Define database target var dbTarget = new DatabaseTarget(); dbTarget.ConnectionStringName = "MyDatabase"; dbTarget.CommandText = "INSERT INTO Log (TimeStamp, Level, Message) VALUES(@TimeStamp, @Level, @Message)"; dbTarget.Parameters.Add(new DatabaseParameterInfo("@TimeStamp", "${date} ${time}")); dbTarget.Parameters.Add(new DatabaseParameterInfo("@Level", "${level}")); dbTarget.Parameters.Add(new DatabaseParameterInfo("@Message", "${message}")); config.AddTarget("db", dbTarget); config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, dbTarget)); LogManager.Configuration = config; Logger.Error("This is an error message for database logging."); } }This example shows how to log a message to a database using the `LogManager`. A `DatabaseTarget` is defined with a connection string and a SQL command to insert the log message into a database table. The logging rule is added to the `LoggingRules` list. Finally, the `Configuration` property is set to the new configuration. In all cases, LogManager is used as the entry point for configuring logging for the application. It is responsible for managing loggers, targets, and filters. The package library used in this example is NLog.