Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / XML

PB-LOG 1.5, quick and easy XML logger

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
30 Apr 2012CPOL2 min read 25.1K   480   14   4
A quick and easy XML logger for .NET (WPF, ASP.NET, WP7, ecc.). You can track your application by entering logs with the associated user.

Introduction

Hi! PB-LOG is a XML logger to track your application by entering logs of varius kinds (Info, Errors, Events and Warning) with the associated user. Now, it's available for .NET and WP7, but in the future I would want develop this project for Java and PHP. With 1.0 version, the old ways to use PB-LOG is still valid. Now, there are more ways to add, remove, check logs and users.

Background

I wanted write this small XML logger because, as each developer, I needed an easy, but powerful XML logger. I found and tried a lot of loggers, but them were difficult and complicated. PB-LOG is very easy to use and under here there's a code example.

Initialize PB-LOG:

C#
Logger logger = new Logger(); // basic - default name of xml file

Logger logger = new Logger(@"C:\logger.xml"); // you can decide name of xml file

Logger logger = new Logger(true) // you can decide if users will be owners of logs. in this way, when will be deleted a user, will be deleted also their logs

Logger logger = new Logger(@"C:\logger.xml", true) // you can decide name of xml file and if users will be owners of logs

For WP7, in the costructor you don't have to write the path, but directly the filename. Like this:

C#
Logger logger = new Logger("logger.xml", true) // you can decide name of xml file and if users will be owners of logs 

Add user:

C#
User user = new User()
{            
   Username = "helloworld",
   Rule = "Administrator"
};

logger.Users.Add(user);

logger.Users.Add("username", "rule");

Add log with and without the user:

C#
Log logUser = new Log() // log with a user
{
   LogType = LogType.Info,
   Message = "loading data...",
   User = user
};

Log logNoUser = new Log() // log without a user
{
   LogType = LogType.Info,
   Message = "loading data..."
};

logger.Logs.Add(logUser);


logger.Logs.Add(logNoUser);


logger.Logs.Add("message", LogType.Fatal);

logger.Logs.Add("message", "username", LogType.Fatal);

XML result:

XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Logs>
  <Errors />
  <Warnings />
  <Informations>
    <Info Message="Hello, world!" Created="08/04/2012 01:58:37" User="pisolobill" />
  </Informations>
  <Events />
  <Users>
    <User Username="pisolobill" Rule="Administrator" Created="08/04/2012 01:58:07" />
  </Users>
</Logs>

Users and Logs are custom collection the implement ICollection<T>. I wanted use this interface because their methods (Add, Remove, Contains, ecc.) are already know. ICollection supports queries LINQ and other methods, like this:

C#
if(logger.Users.Exists(u => u.Username == "username" && u.Rule == "rule"))
{
   logger.Users.Remove("username");
}

Log log = logger.Logs[2];

IEnumerable<Log> getWarnings = logger.Logs.Debugs;

IEnumerable<Log> linq = logger.Logs.Errors.Where(u => u.Rule == "Administrator");

This method will delete the XML file and it will dispose all resources.

How it works

PB-LOG load and write all XML data by XDocument.

Versions

04/11/2012 - PB.LOG - 0.9.7

04/24/2012 - PB.LOG - 1.0 - more features, more easy, resolve some bugs

04/29/2012 - PB.LOG - 1.5 - more fast, fixed few bugs (as in every version), more solid foundations (Thank you Alois Kraus for suggestions)

Contact me:

If you have some ideas, suggestions or other, contact me, please.

Thank you.

This article was originally posted at http://pblog97.codeplex.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
markin7730-Apr-12 8:16
markin7730-Apr-12 8:16 
GeneralRe: My vote of 5 Pin
pisolobill30-Apr-12 8:18
pisolobill30-Apr-12 8:18 
GeneralMy vote of 1 Pin
Alois Kraus25-Apr-12 20:51
Alois Kraus25-Apr-12 20:51 
Sorry to be so rude but this logging library is performance and memory wise horrible. You do keep all logs in a collection and for every log you do write you load the complete Xml document from disc, add the next log node and save it again. Besides this you have poor error handling. The pattern throw ex; will loose the original call stack macking it much harder to find out what went wrong. Please use throw; or simply spare the catch clause if you do nothing else than to rethrow an exception.
GeneralRe: My vote of 1 Pin
pisolobill26-Apr-12 1:49
pisolobill26-Apr-12 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.