Click here to Skip to main content
11,415,164 members (76,237 online)
Click here to Skip to main content
Articles » Web Development » Ajax » General » Downloads
Add your own
alternative version

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

, 22 Jan 2013 CPOL
An article about how to keep AJAX simple as it is and get the most out of it.
ajaxiontest1.0_java_src.zip
AjaxionTest
ajaxionTest.jpr
ajaxionTest.jpr.bak
AjaxionTest.jws
classes
ajaxionTest.cdi
pw
ajaxion
AjaxionEventConsumer.class
log
FileLog.class
servlet
ajaxionTest.class
servlet.cdi
public_html
css
images
processing.gif
ready.jpg
js
WEB-INF
pw.ajaxion
classes
pw
ajaxion
AjaxionEventConsumer.class
AjaxionEventHandler.class
pw.ajaxion.cdi
pw.ajaxion.jpr
src
pw
ajaxion
pw.log
classes
audit.log
pw
log
FileLog.class
pw.log.cdi
test
pw
log
FileLogTest.class
pw.log.jpr
src
pw
log
test
pw
log
src
servlet
ajaxiontest1.0_net1.1_src.zip
Ajaxion
Ajaxion.csproj.user
Ajaxion.ico
js
AjaxionTest.csproj.webinfo
Audit
Audit.csproj.user
Audit.ico
css
Global.asax
images
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
js
ajaxiontest1.0_net2.0_src.zip
Ajaxion.csproj.user
Ajaxion.ico
App_Code
Audit.csproj.user
Audit.ico
Global.asax
AjaxFlavour1.gif
AjaxFlavour2.gif
AjaxFlavour3.jpg
App.ico
HappyAccountant.jpg
processing.gif
ready.JPG
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Diagnostics;

namespace Audit
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class FileLog
	{
		#region Constants & Enumerations

		/// <summary>
		/// Log levels: 0 = Disabled, 1 = Normal, 2 = Enhanced
		/// </summary>
		public enum Level { Disabled, Normal, Enhanced };
		/// <summary>
		/// Log level configuration key
		/// </summary>
		public const string LOG_LEVEL_KEY = "LogLevel";
		/// <summary>
		/// Log file key
		/// </summary>
		public const string LOG_FILE_KEY = "LogFile";

		#endregion // Constants & Enumerations

		#region Static Members

		private const int milisecWait = 1000 * 30;

		private static Mutex mutex = new Mutex();

		private static Level logLevel = Level.Normal;

		private static string logFile = string.Empty;

		#endregion // Static Members

		#region Static Methods

		/// <summary>
		/// The file to log into; if it is string.Empty the assembly name will be used.
		/// It is not thread safe!
		/// </summary>
		public static string LogFile
		{
			get
			{
				return logFile;
			}
			set
			{
				logFile = value;
			}
		}
		/// <summary>
		/// The log level in use to allow the write of log entries
		/// </summary>
		public static Level LogLevel
		{
			get
			{
				return logLevel;
			}
			set
			{
				logLevel = value;
			}
		}

		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public static void WriteLog(string msg)
		{
			try
			{
				mutex.WaitOne(milisecWait, true);

				if (logFile == null || logFile == string.Empty)
				{
					FileLog.logFile = Assembly.GetExecutingAssembly().Location;
					FileLog.logFile = FileLog.logFile.Replace(".dll", ".log");
					FileLog.logFile = FileLog.logFile.Replace(".exe", ".log");
				}
				FileInfo fileInfo = new FileInfo(@logFile);

				// This text is added only once to the file.
				if (fileInfo.Exists)
				{
					using (StreamWriter sw = fileInfo.AppendText())
					{
						sw.Write("");
						sw.Write(msg);
						sw.Close();
					}
				}
				else
				{
					using (StreamWriter sw = fileInfo.CreateText())
					{
						sw.Write("");
						sw.Write(msg);
						sw.Close();
					}
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine("EXCEPTION: " + ex.Message + ex.StackTrace);
			}
			finally
			{
				mutex.ReleaseMutex();
			}
		}
		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public static void WriteLog(string msg, Level level)
		{
			if (logLevel == level)
				WriteLog(msg);
		}

		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public static void WriteLogLn(string msg)
		{
			WriteLog(msg + "\r\n");
		}
		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public static void WriteLogLn(string msg, Level level)
		{
			if (logLevel == level)
				WriteLogLn(msg);
		}

		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public static void WriteLogTrace(Exception ex)
		{
			WriteLogLn(string.Format("EXCEPTION: {0}\r\nTrace: {1}\r\n", ex.Message, ex.StackTrace));
		}
		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public static void WriteLogTrace(Exception ex, Level level)
		{
			if (logLevel == level)
				WriteLogTrace(ex);
		}

		#endregion // Static Methods

		#region Members

		private FileInfo logFileInfo;

		private Level loggingLevel = Level.Normal;

		#endregion // Members

		#region Properties

		/// <summary>
		/// The current logging level (one of 0 = Disabled, 1 = Normal, 2 = Enhanced)
		/// </summary>
		public Level LoggingLevel
		{
			get { return loggingLevel; }
			set { loggingLevel = value; }
		}

		#endregion // Properties

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="logFile">Log file name</param>
		public FileLog(string logFile)
		{
			if (logFile == null || logFile == string.Empty)
			{
				logFile  = Assembly.GetExecutingAssembly().Location;
				logFile = logFile.Replace(".dll", ".log");
				logFile = logFile.Replace(".exe", ".log");
			}
			logFileInfo = new FileInfo(logFile);
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="logFile">Log file name</param>
		/// <param name="logLevel">One of 0 = Disabled, 1 = Normal, 2 = Enhanced</param>
		public FileLog(string logFile, Level level) : this(logFile)
		{
			loggingLevel = level;
		}

		#endregion // Constructor

		#region Methods

		/// <summary>
		/// Writes the given message into log.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public void Write(string msg)
		{
			try
			{
				if (logFileInfo.Exists)
				{
					using (StreamWriter sw = logFileInfo.AppendText())
					{
						sw.Write(string.Empty);
						sw.Write(msg);
						sw.Close();
					}
				}
				else
				{
					using (StreamWriter sw = logFileInfo.CreateText())
					{
						sw.Write(string.Empty);
						sw.Write(msg);
						sw.Close();
					}
					logFileInfo.Refresh();
				}
			}
			catch (Exception ex)
			{
				Debug.WriteLine("EXCEPTION: " + ex.Message + ex.StackTrace);
			}
		}
		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public void Write(string msg, Level level)
		{
			if (logLevel == level)
				Write(msg);
		}

		/// <summary>
		/// Writes the given message into log.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public void WriteLn(string msg)
		{
			Write(msg + "\r\n");
		}
		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public void WriteLn(string msg, Level level)
		{
			if (logLevel == level)
				WriteLn(msg);
		}

		/// <summary>
		/// Writes the given exception and trace into log.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		public void WriteTrace(Exception ex)
		{
			WriteLn(string.Format("EXCEPTION: {0}\r\nTrace: {1}\r\n", ex.Message, ex.StackTrace));
		}	

		/// <summary>
		/// Writes the given message into assembly .log. It is threadsafe.
		/// </summary>
		/// <param name="msg">Message to write.</param>
		/// <param name="level">The log level that allows the log write</param>
		public void WriteTrace(Exception ex, Level level)
		{
			if (logLevel == level)
				WriteTrace(ex);
		}

		#endregion // Methods
	}
}

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

radumi
Software Developer
New Zealand New Zealand
Coder

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.4 | Last Updated 22 Jan 2013
Article Copyright 2008 by radumi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid