Logging
Logging
Logging
Logging is a means of auditing or keeping track of every
activity that has taken place in the application or
system.
Logging
This is crucial as this can tell us the activities that
took place at any particular point in time during the
lifecycle of the application.
Logging
Let’s say that we want to display a message on the
Console “I’ve Started Logging” on the Console but also
to log it to a file. How might we do that?
Logging
With the previous Knowledge, we can simply make a log
file (.txt) and log the contents into that file.
Logging
using System;
using System.IO;
namespace Logging
{
class Program
{
static void Main(string[] args)
{
StreamWriter logger = new StreamWriter("Logger.txt", true);
Console.WriteLine("I've Started Logging");
logger.WriteLine("I've Started Logging");
logger.Close();
Console.ReadKey();
}
}
}
Logging
Go to Your project directory and in the Bin folder.
You’ll see a file (Logger.txt) hanging out there in
addition to your project files.
Logging
This is the basic idea of Logging your program activities
in the file for future reference.
Logging
Logging is the practice of determining what information
is useful to capture and then recording it somewhere
for future access.
Logging
Is that all we need to know about logging?
Of course not..!!
namespace Logging
{
class Program
{
static readonly ILog logger =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Console.WriteLine("I've Started Logging");
XmlConfigurator.Configure();
logger.Info("Info message: I've Started Logging");
Console.ReadKey();
}
}
}
Logging: Step 1 Include these libraries
using System;
using System.IO;
using log4net;
using log4net.Config;
using System.Reflection;
namespace Logging
{
class Program
{
static readonly ILog logger =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Console.WriteLine("I've Started Logging");
XmlConfigurator.Configure();
logger.Info("Info message: I've Started Logging");
Console.ReadKey();
}
}
}
Logging: Step 2 Declare this Variable
using System;
using System.IO;
using log4net;
using log4net.Config;
using System.Reflection;
namespace Logging
{
class Program
{
static readonly ILog logger =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Console.WriteLine("I've Started Logging");
XmlConfigurator.Configure();
logger.Info("Info message: I've Started Logging");
Console.ReadKey();
}
}
}
Logging: Step 3 Log the information
using System;
using System.IO;
using log4net;
using log4net.Config;
using System.Reflection;
namespace Logging
{
class Program
{
static readonly ILog logger =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
Console.WriteLine("I've Started Logging");
XmlConfigurator.Configure();
logger.Info("Info message: I've Started Logging");
Console.ReadKey();
}
}
}
Logging
myLoggerFile.log is created in the bin folder.
Logging
This is just the basic on Logging.
https://www.papertrail.com/solution/tips/7-best-practic
es-for-c-logging-with-examples/
Conclusion
● Logging is necessary for the development and operations
teams to track down and fix bugs quickly
● Instead of reinventing the wheel, we can use an existing
logging framework such as log4net.
● Use context-rich logging so that we’ll have all the information
we might need when troubleshooting.
Learning Objective