System Logger Log is a logging feature in C# which allows developers to record events and messages during program execution. It helps in debugging and troubleshooting issues that may occur in the application.
Example:
1) Basic Logging
using System;
using Microsoft.Extensions.Logging;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddConsole()
.AddDebug();
});
ILogger logger = loggerFactory.CreateLogger();
logger.LogInformation("This is a formatted message {0}", "with arguments");
}
}
}
In the above example, we are creating a logger object using LoggerFactory and adding Console and Debug output providers. Then we are creating a logger object and writing a log message to the console.
Package Library: Microsoft.Extensions.Logging
2) Logging to a file
using System;
using System.IO;
using Microsoft.Extensions.Logging;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var loggerFactory = new LoggerFactory().AddFile("C:\\Logs\\log.txt");
ILogger logger = loggerFactory.CreateLogger();
logger.LogInformation("This is a log message written to the file");
}
}
}
In the above example, we are adding a file output provider to the logger object. Then we are creating a logger object and writing a log message to a text file.
Both examples use the Microsoft.Extensions.Logging package library which provides a common interface for logging in .NET applications.
C# (CSharp) System Logger.Log - 22 examples found. These are the top rated real world C# (CSharp) examples of System.Logger.Log extracted from open source projects. You can rate examples to help us improve the quality of examples.