0% found this document useful (0 votes)
6 views27 pages

Logging

m

Uploaded by

tiwano5319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

Logging

m

Uploaded by

tiwano5319
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

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..!!

This is a lot more goes into a proper logging strategy


than just randomly dumping text.
Logging
Let’s see some of the components that go into creating
entries in your log.

Conceptually, you can think of each entry in a log as an


event.
Logging
Here are some things that you’ll usually want to capture for each
event:
● A unique identifier for the event.
● A timestamp. Exactly when did this event take place?
● Context that makes it clear to a reader what’s going on. For
instance, just recording “I’ve Started Logging” might prove
confusing weeks or months later. A better message, including
context, might say, “Recorded the activity at 23 April 2022
of ‘I’ve Started Logging'”
● Tags and categories for each entry for later search and
classification.
● Log levels, such as “error,” “warning,” or “information,” that
allow further filtering and provide additional context.
Logging: Log4Net Package
● We do not have to do that all by ourselves.
Programmers have provided us packages for logging.
● One such package is Log4Net.
Logging: Log4Net Package
● In order to install the Package, right
click on the references in Solution
Explorer Window
Logging: Log4Net Package
● Then Click on the Manage NuGet
Packages.
Logging: Log4Net Package
● Then Search Log4Net and install the package.
Logging: Log4Net Package
● After installing you have to Configure your
App.config File. Click on the App.Config and
following file will open
Logging: Log4Net Package
● Save the following within configuration tag.
Logging: Log4Net Package
● Save the following within configuration tag.
Logging: Code
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 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.

For further reading, Explore this link:

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

Log the information in the


Programs to trace down the
changes while the execution of
Program.

You might also like