Click here to Skip to main content
Click here to Skip to main content

PB-LOG 1.5, quick and easy XML logger

, 30 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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:

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:

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

Add user:

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

logger.Users.Add(user);

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

Add log with and without the user:

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 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:

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.

License

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

Share

About the Author

pisolobill

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermarkin7730-Apr-12 8:16 
GeneralRe: My vote of 5 Pinmemberpisolobill30-Apr-12 8:18 
GeneralMy vote of 1 PinmemberAlois Kraus25-Apr-12 20:51 
GeneralRe: My vote of 1 Pinmemberpisolobill26-Apr-12 1:49 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 30 Apr 2012
Article Copyright 2012 by pisolobill
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid