- ajaxiontest1.0_net1.1_src.zip
- AjaxionTest
- ajaxiontest1.0_net2.0_src.zip
- ajaxiontest1.0_java_src.zip
- ajaxionTest.jpr
- ajaxionTest.jpr.bak
- AjaxionTest.jws
- classes
- ajaxionTest.cdi
- pw
- ajaxion
- AjaxionEventConsumer.class
- log
- FileLog.class
- servlet.cdi
- servlet
- ajaxionTest.class
- public_html
- pw.ajaxion
- classes
- pw.ajaxion.cdi
- pw
- ajaxion
- AjaxionEventConsumer.class
- AjaxionEventHandler.class
- pw.ajaxion.jpr
- src
- pw
- ajaxion
- pw.log
- classes
- audit.log
- pw.log.cdi
- pw
- log
- FileLog.class
- test
- pw.log.jpr
- src
- src
- servlet
|
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.