I've been using the following in a web page and it seems to work ok.
It gets the latest file created by the rolling appender.
I'm not sure that this is adding anything that you don't already know.
-----------------------------------
           try
           {
               string logDir = ConfigurationManager.AppSettings["LogDir"];
               DirectoryInfo dirInfo = new DirectoryInfo(logDir);

string logFileNameRoot = ConfigurationManager.AppSettings["LogFileNameRoot"]; FileInfo[] fileInfoArray = dirInfo.GetFiles(logFileNameRoot + "*.*", SearchOption.TopDirectoryOnly);

Array.Sort(fileInfoArray, new ComparableFileInfo(UtilityFunctions.CompareByOptions.LastWriteTimeDescending));

               if (fileInfoArray.Length > 0)
               {
                   StreamReader st = fileInfoArray[0].OpenText();
                   string line = st.ReadLine();
                   do
                   {
                       Response.Write(line + "<p>");
                       //Response.Write(line + "<p>");
                       line = st.ReadLine();
                   } while (!st.EndOfStream);

               }
           }
           catch
           {
           }
           finally
           {
               Response.End();
           }
It uses:
--------------------------
   public enum CompareByOptions
   {
       FileNameAscending,
       FileNameDescending,
       LastWriteTimeAscending,
       LastWriteTimeDescending,
       LengthAscending,
       LengthDescending
   }

   public class ComparableFileInfo : IComparer
   {
       CompareByOptions compareBy = CompareByOptions.FileNameAscending;

       public ComparableFileInfo(CompareByOptions cBy)
       {
           compareBy = cBy;
       }

#region IComparer Members

       int  IComparer.Compare(object x, object y)
       {
           FileInfo f1 = (FileInfo)x;
           FileInfo f2 = (FileInfo)y;
           switch (compareBy)
           {
               case CompareByOptions.FileNameAscending:
                   return string.Compare(f1.Name, f2.Name);
               case CompareByOptions.FileNameDescending:
                   return string.Compare(f2.Name, f1.Name);
               case CompareByOptions.LastWriteTimeAscending:
return DateTime.Compare(f1.LastWriteTime, f2.LastWriteTime);
               case CompareByOptions.LastWriteTimeDescending:
return DateTime.Compare(f2.LastWriteTime, f1.LastWriteTime);
               case CompareByOptions.LengthAscending:
                   return (int)(f1.Length - f2.Length);
               case CompareByOptions.LengthDescending:
                   return (int)(f2.Length - f1.Length);
           }
           return -1;
       }
#endregion
   }
-------------------------------------------------------------

Dan Essin


Price, Randall wrote:

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?

Thanks,

*Randall Price*

Secure Enterprise Technology Initiatives

Microsoft Implementation Group

Virginia Tech Information Technology

1700 Pratt Drive

Blacksburg, VA  24060

Email:      [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>

Phone:     (540) 231-4396

------------------------------------------------------------------------

*From:* Price, Randall [mailto:[EMAIL PROTECTED]
*Sent:* Wednesday, April 04, 2007 4:38 PM
*To:* [email protected]
*Subject:* FileAppender+MinimalLock not working as expected

Hello,

I have an WinForms application in C# that generates data files for a graphing application. This application uses log4net to log progress, information, errors, etc. I also want this application to be able to view the log file. When I try to open the log file I get the following error message:

The process cannot access the file "C:\....\MyApplication.log" because it is being used by another process.

I searched Google and found that I should add the following line to my configuration file:

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

Adding this does not seem to allow access to log file. I expected to be able to open the log file in Notepad by double-clicking on it but that also generates the same error. I want to be able to have my application open the log file and display the contents in a TextBox but I get the same error when I try that as well.

I am using:

Visual Studio 2005 Professional

Microsoft .NET Framework 2.0

log4net v1.2.10.0

Windows XP Pro w/SP2

Here is my configuration file:

<?xml version="1.0" encoding="utf-8" ?>

<log4net>

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

    <file value="MyApplication.log" />

    <appendToFile value="true" />

    <maximumFileSize value="100KB" />

    <maxSizeRollBackups value="2" />

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

    <layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />

    </layout>

  </appender>

  <root>

    <level value="DEBUG" />

    <appender-ref ref="RollingFile" />

  </root>

</log4net>

Am I missing something here?

Thanks,

*Randall Price*

Secure Enterprise Technology Initiatives

Microsoft Implementation Group

Virginia Tech Information Technology

1700 Pratt Drive

Blacksburg, VA  24060

Email:      [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>

Phone:     (540) 231-4396

Reply via email to