Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

NLog Log and Audit Advanced Target

, 26 May 2010
A way to audit your business objects using NLog.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace App.Logger.Targets.ObjectHistoryLogger.Configuration
{
	public class XMLConfiguration : List<XMLTypeConfiguration>
	{
		OHTarget target = null;
		public XMLConfiguration(OHTarget target)
		{
			this.target = target;
			ReadXml();
		}

		public XMLTypeConfiguration GetTypeConfiguration(Type type)
		{
			var q = (from cfg in this
					 where (string.IsNullOrEmpty(cfg.TypeName) ? string.Empty : cfg.TypeName) == type.FullName.ToString()
					select cfg).ToList();

			if (q.Count() == 1)
			{
				return q[0];
			}
			else if (q.Count() >= 1)
			{
				throw new MultipleXmlConfigurationsForTheSameTypeException();
			}

			return null;
		}
		


		private void ReadXml()
		{
			string LogTypes = target.LogTypes;

			if (!string.IsNullOrEmpty(LogTypes))
			{
				LogTypes = LogTypes.Trim();
				string[] types = LogTypes.Split('|');

				foreach (string type in types)
				{
					string typeName = string.Empty;
					string keyProperty = string.Empty;
					List<string> properties = new List<string>();

					string[] items = type.Split(';');

					foreach (string item in items)
					{
						string[] cfgP = item.Split(':');
						if (cfgP != null && cfgP.Length == 2)
						{
							switch (cfgP[0].ToLower())
							{
								case "name":
									typeName = cfgP[1];
									break;
								case "key":
									keyProperty = cfgP[1];
									break;
								case "properties":
									string[] props = cfgP[1].Split(',');
									foreach (string prop in props)
									{
										properties.Add(prop);
									}
									break;
								default:
									break;
							}
						}
					}

					XMLTypeConfiguration typeConfig = new XMLTypeConfiguration(typeName, keyProperty, properties);
					this.Add(typeConfig);
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

About the Author

AlexCode
Architect
Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.
 
Find more at on my blog.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 26 May 2010
Article Copyright 2010 by AlexCode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid