You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by elavarasan <el...@infravio.com> on 2004/07/02 06:44:20 UTC

Unable to tail log while writing it


Hi,

I am trying to read the log file while another thread writes to it.

 But it gives the following  ERROR Message,

" An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file "C:\Documents and
Settings\elavarasan\Desktop\Agent\TxtLog\Logging\LogFiles\log.txt" because
it is being used

by another process. "

Environment:

WinXP SP1

MS Dot Net Framework v 1.1.4322

Log4Net version 1.1 - using RollingFileAppender to log messages.

Single process with two threads - One writes to a log file. Another reads it
and pumps read log messages on to the Console.

Looking at the source reveals that the FileAppender opens the log file using
StreamWriter(filename, append_flag, m_encoding). There is no mention of any
sort of sharing.

Would that be a problem?

Any help to help me trace out the problem would be greatly appreciated.


This is the code i have used..
using System;
using System.IO;
using System.Threading;
using log4net;
using log4net.Config;

namespace Logging
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Logging
{
static ILog LOGGER = log4net.LogManager.GetLogger(typeof(Logging));
static void log()
{
string message = "my log";
for (int i = 0; i < 1000; i++)
{
LOGGER.Info( i + message);
}
}
static void read()
{
string stFileNamePath=@"C:\Documents and
Settings\elavarasan\Desktop\Agent\TxtLog\Logging\LogFiles\log.txt";
FileStream objectFileStram = new FileStream( stFileNamePath
,FileMode.Open,FileAccess.Read,FileShare.Read);
StreamReader objectStreamReader = new StreamReader(objectFileStram);
Console.Write(" This is From reader " + objectStreamReader.ReadToEnd());
objectStreamReader.Close();
objectFileStram.Close();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
log4net.Config.DOMConfigurator.Configure(new
FileInfo(@"C:\Temp\Logging\log4net.xml"));
for (int i = 0; i < 10; i++)
{
Thread myThread = new Thread(new ThreadStart(log));
Thread myReadThread = new Thread(new ThreadStart(read));
myThread.Start();
myReadThread.Start();
}
Console.ReadLine();
}
}
}


Thanks,

Elavarasan.