Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / Windows Forms

NLog Log and Audit Advanced Target

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
26 May 2010CPOL6 min read 42.5K   750   35  
A way to audit your business objects using NLog.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;

namespace App.Logger.Targets.ObjectHistoryLogger.Data.DBScripts
{

    public static class OHScriptManager
	{

		/// <summary>
		/// Gets the tables scripts ordered by version, older first.
		/// </summary>
		/// <returns></returns>
		public static List<string> GetTablesScripts(OHDbBaseClient dbClient)
		{
			return GetScripts(ScriptTypes.Tables, dbClient);
		}

		/// <summary>
		/// Gets the procedures scripts ordered by version, older first.
		/// </summary>
		/// <returns></returns>
		public static List<string> GetProceduresScripts(OHDbBaseClient dbClient)
		{
			return GetScripts(ScriptTypes.Procedures, dbClient);
		}

		/// <summary>
		/// Gets the views scripts ordered by version, older first.
		/// </summary>
		/// <returns></returns>
		public static List<string> GetViewsScripts(OHDbBaseClient dbClient)
		{
			return GetScripts(ScriptTypes.Views, dbClient);
		}

		/// <summary>
		/// Gets the data scripts ordered by version, older first.
		/// </summary>
		/// <returns></returns>
		public static List<string> GetDataScripts(OHDbBaseClient dbClient)
		{
			return GetScripts(ScriptTypes.Data, dbClient);
		}

		/// <summary>
		/// Supported script types
		/// </summary>
		public enum ScriptTypes
		{
			/// <summary>
			/// Table schema script.
			/// </summary>
			Tables = 1,
			/// <summary>
			/// Stored Procedures script.
			/// </summary>
			Procedures = 2,
			/// <summary>
			/// Views script.
			/// </summary>
			Views = 3,
			/// <summary>
			/// Data scripts.
			/// </summary>
			Data = 4
		}

		/// <summary>
		/// Gets the scripts by type and ordered by version, older first.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns></returns>
		private static List<string> GetScripts(ScriptTypes type, OHDbBaseClient dbClient)
		{
			string ObjectsPrefix = dbClient.target.DBTablesPrefix;
			string _dbScriptsContainerPath = string.IsNullOrEmpty(dbClient.DbScriptsContainerPath) ? string.Empty : dbClient.DbScriptsContainerPath;
			
			string filter = string.Empty;
			switch (type)
			{
				case ScriptTypes.Tables:
					filter = "tables-v";
					break;
				case ScriptTypes.Procedures:
					filter = "procedures-v";
					break;
				case ScriptTypes.Views:
					filter = "views-v";
					break;
				case ScriptTypes.Data:
					filter = "data-v";
					break;
				default:
					break;
			}
			
			Assembly asm = Assembly.GetExecutingAssembly();
			string[] resNames = asm.GetManifestResourceNames();
			Stream sqlStream = null;

			// get all the file names into a list
			List<string> list = new List<string>();
			if (resNames == null || resNames.Length == 0) return list;
			for (int i = 0; i < resNames.Length; i++)
			{
				if (resNames[i].StartsWith(_dbScriptsContainerPath) && resNames[i].ToLower().Contains(filter.ToLower()))
					list.Add(resNames[i]);
			}

			// sort the list by name considering that the name contains a version number
			list.Sort(new ScriptFileNameComparer());

			for (int i = 0; i < list.Count; i++)
			{
				sqlStream = asm.GetManifestResourceStream(list[i]);
				using (StreamReader sr = new StreamReader(sqlStream))
				{
					string script = sr.ReadToEnd();						// read the content of the file
					list[i] = string.Format(script, ObjectsPrefix);		// replace the name of the resource file with its content
				}
			}

			return list;
		}

	}

}

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)


Written By
Architect
Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.

Find more at on my blog.

Comments and Discussions