You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by Valentina Ichimov <vi...@gmail.com> on 2015/09/10 11:33:23 UTC

log4net 1.2.13 Custom Header

Hello,



I tried to set for my RollingLogFile a custom header. Added only in the
head of file, once.

My application is installed in a web farm environment and I had issues
there. Multiple files were created(I used minimal lock as locking model).



Ex:

infoFile_2015_09_03.tsv

infoFile_2015_09_03.tsv2015_09_03.tsv

infoFile_2015_09_03.tsv2015_09_03.tsv2015_09_03.tsv





This is my code:

                /// <summary>

                /// Custom class for override the log4net Header Writing

                /// </summary>



                public class CustomHeaderLogFile : RollingFileAppender

                {

                                protected override void WriteHeader()

                                {

                                                if
(LockingModel.AcquireLock().Length == 0)

                                                {


base.WriteHeader();

                                                }

                                }

                }



                /// <summary>

                /// Custom class for log4net Header Pattern

                /// </summary>

                public class CustomHeaderPatternLayout : PatternLayout

                {

                                public override string Header

                                {

                                                get

                                                {


StringBuilder headerBuilder = new StringBuilder();


headerBuilder.AppendLine("Text \tSource \tTarget \tLocation \tResults
\tUserAgent \tUserIP");



                                                                return
headerBuilder.ToString();

                                                }

                                }

                }



And this is configuration:



<appender name="RollingStatsLogFileAppender" type=" CustomHeaderLogFile">

                                                <file
type="log4net.Util.PatternString" value="C:\\Logs\\infoFile_"/>

                                                <appendToFile value="true"/>

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

                                                <datePattern
value="yyyy'_'MM'_'dd'.tsv'"/>

                                                <rollingStyle
value="Date"/>

                                                <encoding value="utf-8"/>

                                                <layout
type="CustomHeaderPatternLayout">


<conversionPattern value="%message%newline" />

                                                </layout>

                                                <staticLogFileName
value="false"/>

                                </appender>



There was something like different processes created a file for each.

When I enabled debugging I saw in the errors : log4net:ERROR
[CustomHeaderLogFile] ErrorCode: GenericFailure. Unable to acquire lock on
file C:\\Logs\\infoFile... The process cannot access the file .. because it
is being used by another process.



Can you help me with this issue?

I need to have a single file, with custom header, rolling by date, current
file should contain the current date.

Ex:

infoFile_2015_09_03.tsv

infoFile_2015_09_04.tsv

infoFile_2015_09_05.tsv





Thank you,

Vali