My solution only works for one appender only and the counter will be reset every time you start the application.
You could use a pattern converter:
public sealed class LineCounterPatternConverter : PatternLayoutConverter
{
private static int _counter = 0;
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
var counter = System.Threading.Interlocked.Increment(ref _counter);
writer.Write(counter.ToString());
}
}
then you configure it like this:
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%LC] %message%newline" />
<converter>
<name value="LC" />
<type value="YourNameSpace.LineCounterPatternConverter" />
</converter>
</layout>
The [] brackets are of course optional. You could also do something like this %6LC
which would pad the line with spaces so that you get the message aligned nicely.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…