- apploggerdemo2.zip
- AppLoggerDemo2
- AppLogger
- AppLogger.BusinessFacade.dll
- AppLogger.BusinessRules.dll
- AppLogger.DataAccess.dll
- AppLogger.exe
- AppLogger.exe.config
- AppLoggerTest
- applogger2.zip
- AppLogger2
- AppLogger.etp
- AppLogger.etp.eto
- AppLogger.sln
- AppLoggerTest
- Assemblies
- Assemblies.csproj
- Assemblies.csproj.eto
- Assemblies.csproj.user
- bin
- AppLogger.BusinessFacade.dll
- AppLogger.BusinessRules.dll
- AppLogger.DataAccess.dll
- AppLogger.exe
- AppLogger.exe.config
- InstallUtil.InstallLog
- BusinessFacade
- BusinessRules
- DataAccess
- SystemFrameworks
|
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace AppLogger.DataAccess
{
/// <summary>
/// This class represents a thread-safe log file.
/// It contains a file stream and an associated lock.
/// </summary>
public class LogFile
{
/// <summary>
/// This is for synchronizing the log file.
/// </summary>
private object m_objSyncLock = new object();
/// <summary>
/// Holds the log file stream.
/// </summary>
private FileStream m_objFileStream;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="strFileName">The log file name</param>
public LogFile(string strFileName)
{
m_objFileStream = new FileStream(strFileName,
FileMode.Append,
FileAccess.Write,
FileShare.Read);
}
/// <summary>
/// Get the object for synchronizing this instance's FileStream object.
/// </summary>
/// <returns>An object</returns>
public object GetSyncLock()
{
return m_objSyncLock;
}
/// <summary>
/// Get the file.
/// </summary>
/// <returns>The FileStream object</returns>
public FileStream GetFileStream()
{
return m_objFileStream;
}
}
/// <summary>
/// This is a singleton class that provides thread-safe writing of log files.
/// </summary>
public class LogFileDataWriter
{
/// <summary>
/// Holds the singleton instance.
/// </summary>
private static LogFileDataWriter m_objInstance = new LogFileDataWriter();
/// <summary>
/// Contains a table of AppName(key) and LogFile(value).
/// </summary>
private Hashtable m_logFiles = new Hashtable();
/// <summary>
/// Singleton Constructor
/// </summary>
private LogFileDataWriter()
{
}
/// <summary>
/// Get the file stream or create it if not found.
/// </summary>
private LogFile GetLogFile(string strAppName)
{
LogFile objLogFile;
if (!m_logFiles.ContainsKey(strAppName))
{
objLogFile = new LogFile(strAppName + ".log");
m_logFiles.Add(strAppName, objLogFile);
return objLogFile;
}
objLogFile = (LogFile)m_logFiles[strAppName];
return objLogFile;
}
/// <summary>
/// Get this singleton
/// </summary>
/// <returns>This LogFileDataWriter instance</returns>
public static LogFileDataWriter GetInstance()
{
return m_objInstance;
}
/// <summary>
/// Writes the message to log file.
/// </summary>
/// <param name="strSource">The source of the message</param>
/// <param name="strMessage">The message content</param>
public void PersistMessage(string strSource, string strMessage)
{
LogFile objLogFile = GetLogFile(strSource);
lock(objLogFile.GetSyncLock())
{
FileStream objStream = objLogFile.GetFileStream();
if (!objStream.CanWrite)
{
string strFileName = objStream.Name;
objStream.Close();
objStream = new FileStream(strFileName,
FileMode.Append,
FileAccess.Write,
FileShare.Read);
}
StreamWriter objWriter = new StreamWriter(objStream);
objWriter.WriteLine(strMessage);
objWriter.Flush();
objWriter.Close();
}
}
}
}
|
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.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
A software craftman, the roads I travelled include C++ to Java to C#, from Windows to Unix and now Microsoft .NET.
Full-time, I play the pragmatic roles of software designer, engineer and architect in the programming trenches.