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 CPOL
A way to audit your business objects using NLog.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NLog;
using App.Logger.Targets.ObjectHistoryLogger;
using App.Logger.Targets.ObjectHistoryLogger.Data;
using App.Logger.WebTests.BusinessObjects;
using System.Drawing;
using System.Data;

namespace App.Logger.WebTests
{
	public partial class _Default : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void btnAddCustomer_Click(object sender, EventArgs e)
		{
			NLog.Logger logger = LogManager.GetCurrentClassLogger();

			for (int i = 0; i < 1; i++)
			{
				Customer customer = new Customer()
				{
					CustomerID = 2,
					Name = "<<<<<<zzzzzzzzz " + i.ToString(),
					Address = "bla akljdhakjdshakjdsh...",
					MobilePhone = "123091328",
					StartDate = new DateTime(2004, 1, 25)
				};

				// this is what we need to do. 
				// Create a new instance of OHInfo passing the object to be logged and call the NLog logger normally.
				// use whatever level you want just make sure you're logging that level on the configuration. 
				logger.Trace(customer);
				//logger.Info(string.Empty, customer, new OHAuditInfo() { UserName="Admin" });	

				string strItems = "items1, item2, item3";
				string[] items = strItems.Split(',');
				List<string> ItemsList = new List<string>(items);

			}
		}

		protected void btnAddSupplier_Click(object sender, EventArgs e)
		{
			NLog.Logger logger = LogManager.GetCurrentClassLogger();

			for (int i = 0; i < 1; i++)
			{
				Supplier supplier = new Supplier()
				{
					CompanyName="AlexCode, Inc",
					Balance= 131233.23M,
					InvoicingAddress="zzzzzzzzzzz 123, q0waldsk",
					SupplierID=4
				};

				// this is what we need to do. 
				// Create a new instance of OHInfo passing the object to be logged and call the NLog logger normally.
				logger.Info(string.Empty, supplier, new OHAuditInfo() { UserName = "Admin" });	// use whatever level you want just make sure you're logging that level on the configuration. 
			}
		}

		protected void btnGetHistory_Click(object sender, EventArgs e)
		{
			List<OHTarget> OHTargets = App.Logger.NLogUtilities.GetOHTargets();
			foreach (var target in OHTargets)
			{
				OHDbBaseClient dbClient = OHDbFactory.GetDbClient(target);

				gridResult.DataSource = dbClient.GetHistory(typeof(Supplier), "4").AsDataTable();
				gridResult.DataBind();
				break;
			}
		}

		protected void btnGetLastVersion_Click(object sender, EventArgs e)
		{
			List<OHTarget> OHTargets = App.Logger.NLogUtilities.GetOHTargets();
			foreach (var target in OHTargets)
			{
				OHDbBaseClient dbClient = OHDbFactory.GetDbClient(target);
				gridResult.DataSource = dbClient.GetLastVersion(typeof(Customer), "2").AsDataTable();
				gridResult.DataBind();
				break;
			}
		}

		protected void btnGetVersion_Click(object sender, EventArgs e)
		{
			List<OHTarget> OHTargets = App.Logger.NLogUtilities.GetOHTargets();
			foreach (var target in OHTargets)
			{
				OHDbBaseClient dbClient = OHDbFactory.GetDbClient(target);
				gridResult.DataSource = dbClient.GetVersion(typeof(Customer), "2", new DateTime(2010, 5, 22, 3, 0, 0)).AsDataTable();
				gridResult.DataBind();
				break;
			}
		}

		protected void btnCreateSchema_Click(object sender, EventArgs e)
		{
			List<OHTarget> OHTargets = App.Logger.NLogUtilities.GetOHTargets();
			foreach (var target in OHTargets)
			{
				OHDbBaseClient dbClient = OHDbFactory.GetDbClient(target);
				dbClient.CreateDBSchema();
				break;
			}
		}

		protected void btnReadXmlConfiguration_Click(object sender, EventArgs e)
		{
			List<OHTarget> OHTargets = App.Logger.NLogUtilities.GetOHTargets();
			foreach (var target in OHTargets)
			{
				var xmlConfig = target.XmlConfiguration();
			}
		}

	}
}

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)

Share

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 | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 26 May 2010
Article Copyright 2010 by AlexCode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid