Loading...

log4net-user@logging.apache.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: FileAppender+MinimalLock not working as expected - FIXED!! Ron Grabowski Fri Apr 06 00:01:51 2007

This class uses a custom FileStream for writing:

public class ReadWriteTextWriterAppender : TextWriterAppender
{
    private string file;

    public override void ActivateOptions()
    {
        QuietWriter = new QuietTextWriter(
            new StreamWriter(
                new FileStream(File ,FileMode.Append, FileAccess.Write, 
FileShare.ReadWrite)),
                new OnlyOnceErrorHandler());

        Layout = new SimpleLayout();
    }

    public string File
    {
        get { return this.file; }
        set { this.file = value; }
    }
}

I was able to write to the appender then read the contents of the file using 
this code:

XmlConfigurator.ConfigureAndWatch(
    new FileInfo(
    AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
    "log4net.config"));

ReadWriteTextWriterAppender readWriter = new ReadWriteTextWriterAppender();
readWriter.File = @"C:\Ron.txt";
readWriter.ActivateOptions();
((Hierarchy) LogManager.GetRepository()).Root.AddAppender(readWriter);

log.Info("Application started");

string ronText;
using (FileStream fs = new 
FileStream(readWriter.File,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
    using (StreamReader sr = new StreamReader(fs))
    {
        ronText = sr.ReadToEnd();
    }
}

Is that what you're trying to do?

----- Original Message ----
From: "Price, Randall" <[EMAIL PROTECTED]>
To: Log4NET User <[EMAIL PROTECTED]>
Sent: Thursday, April 5, 2007 3:37:54 PM
Subject: RE: FileAppender+MinimalLock not working as expected - FIXED!!

Marc,

Thanks for your response but I could not read the log file when I
removed the MinimalLock entry from the config file and tried using the
File.OpenRead() statement you recommended.

But it was worth a shot...

Randall Price


-----Original Message-----
From: Marc Lewandowski [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 05, 2007 12:37 PM
To: 'Log4NET User'
Subject: RE: FileAppender+MinimalLock not working as expected - FIXED!!

I don't think the problem was really the MinimalLock at all
(ultimately).
For instance, I can view a log file in Notepad, and it will continue to
be
updated, without using MinimalLock (confirm this for yourself).

The problem is how you are opening the file.  I believe the StreamReader
constructor attempts to gain exclusivity on the file by default (from MS
docs for File.OpenText(): "This method is equivalent to StreamReader(
string
)").  Try:

StreamReader sr = new StreamReader( File.OpenRead( strFileName ) );

instead, even without the MinimalLock.  Let us know how you make out.

-Marc



-----Original Message-----
From: Price, Randall [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 05, 2007 12:05 PM
To: Log4NET User
Subject: RE: FileAppender+MinimalLock not working as expected - FIXED!!


FYI,

I got this working by fixing a typo in the config file.  I had

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock\" />

When I should have had

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

Notice the stray backslash at the end -- that came from a cut and paste
from
an article I found on Google that had the stray backslash in the code.
Now
I can successfully have my application open and display its own log
file.

Guess this has been a blonde Monday for me!!

Thanks for all who responded.

Randall Price


-----Original Message-----
From: Price, Randall [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 05, 2007 11:58 AM
To: Log4NET User
Subject: RE: FileAppender+MinimalLock not working as expected

An update on this...

I noticed that I can have the log file open in Notepad and run my
application with no problem.  The log file gets updated even with it
open in
Notepad!!  When I close Notepad and then reopen the log file I can see
the
new log entries.

However, I can still not get my application to open its log file.  I
still
get the error "process cannot access ... being used by another process".

Does anyone have an application working that can view its own log file
or
know how to get this to work?

Thanks,

Randall Price



-----Original Message-----
From: Price, Randall [mailto:[EMAIL PROTECTED] 
Sent: Thursday, April 05, 2007 9:24 AM
To: Log4NET User
Subject: RE: FileAppender+MinimalLock not working as expected -
Correction!

Here is the code to open and read the log file:

    StreamReader sr = new StreamReader(strFileName);
    string strData  = sr.ReadToEnd();
    sr.Close();
    txtData.Text = strData;

As you can see I am not doing anything complex, just opening the file
and
reading the contents into a TextBox.

Randall Price


-----Original Message-----
From: Ron Grabowski [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, April 04, 2007 6:15 PM
To: Log4NET User
Subject: Re: FileAppender+MinimalLock not working as expected -
Correction!

How are you trying to open the file in code?


----- Original Message ----

From: "Price, Randall" <[EMAIL PROTECTED]>

To: Log4NET User <[EMAIL PROTECTED]>

Sent: Wednesday, April 4, 2007 4:49:27 PM

Subject: RE: FileAppender+MinimalLock not working as expected -
Correction!


   When my WinForms application is running I CAN double-click on the log
file from Windows Explorer and open the log file in Notepad.

     

   But when I click my View Log File button on my application, I get the
"process cannot access ... being used by another process" error message.
However process should be the same process making the request to open
the
file for reading.

     

   Has anyone ever done something like this before?