Skip to content

Commit 77b9f85

Browse files
authored
Merge pull request #92 from komsa-ag/Feature/NoLock
2 parents 9ba9452 + 4b027be commit 77b9f85

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

src/log4net/Appender/FileAppender.cs

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public LockStateException(string message, Exception innerException) : base(messa
160160
}
161161

162162
#if !NETCR && !NETSTANDARD1_3
163-
private LockStateException(SerializationInfo info, StreamingContext context) : base(info, context)
163+
private LockStateException(SerializationInfo info, StreamingContext context) : base(info, context)
164164
{
165165
}
166166
#endif
@@ -494,6 +494,7 @@ public FileAppender CurrentAppender
494494
/// <returns></returns>
495495
protected Stream CreateStream(string filename, bool append, FileShare fileShare)
496496
{
497+
filename = Environment.ExpandEnvironmentVariables(filename);
497498
using (CurrentAppender.SecurityContext.Impersonate(this))
498499
{
499500
// Ensure that the directory structure exists
@@ -896,6 +897,118 @@ public override void OnClose()
896897
}
897898
#endif
898899

900+
/// <summary>
901+
/// Hold no lock on the output file
902+
/// </summary>
903+
/// <remarks>
904+
/// <para>
905+
/// Open the file once and hold it open until <see cref="CloseFile"/> is called.
906+
/// Maintains no lock on the file during this time.
907+
/// </para>
908+
/// </remarks>
909+
public class NoLock : LockingModelBase
910+
{
911+
private Stream m_stream = null;
912+
913+
/// <summary>
914+
/// Open the file specified and prepare for logging.
915+
/// </summary>
916+
/// <param name="filename">The filename to use</param>
917+
/// <param name="append">Whether to append to the file, or overwrite</param>
918+
/// <param name="encoding">The encoding to use</param>
919+
/// <remarks>
920+
/// <para>
921+
/// Open the file specified and prepare for logging.
922+
/// No writes will be made until <see cref="AcquireLock"/> is called.
923+
/// Must be called before any calls to <see cref="AcquireLock"/>,
924+
/// <see cref="ReleaseLock"/> and <see cref="CloseFile"/>.
925+
/// </para>
926+
/// </remarks>
927+
public override void OpenFile(string filename, bool append, Encoding encoding)
928+
{
929+
try
930+
{
931+
// no lock
932+
m_stream = CreateStream(filename, append, FileShare.ReadWrite);
933+
}
934+
catch (Exception e1)
935+
{
936+
CurrentAppender.ErrorHandler.Error("Unable to acquire lock on file " + filename + ". " + e1.Message);
937+
}
938+
}
939+
940+
/// <summary>
941+
/// Close the file
942+
/// </summary>
943+
/// <remarks>
944+
/// <para>
945+
/// Close the file. No further writes will be made.
946+
/// </para>
947+
/// </remarks>
948+
public override void CloseFile()
949+
{
950+
CloseStream(m_stream);
951+
m_stream = null;
952+
}
953+
954+
/// <summary>
955+
/// Acquire the lock on the file
956+
/// </summary>
957+
/// <returns>A stream that is ready to be written to.</returns>
958+
/// <remarks>
959+
/// <para>
960+
/// Does nothing. The lock is already taken
961+
/// </para>
962+
/// </remarks>
963+
public override Stream AcquireLock()
964+
{
965+
return m_stream;
966+
}
967+
968+
/// <summary>
969+
/// Release the lock on the file
970+
/// </summary>
971+
/// <remarks>
972+
/// <para>
973+
/// Does nothing. The lock will be released when the file is closed.
974+
/// </para>
975+
/// </remarks>
976+
public override void ReleaseLock()
977+
{
978+
// NOP
979+
}
980+
981+
/// <summary>
982+
/// Initializes all resources used by this locking model.
983+
/// </summary>
984+
public override void ActivateOptions()
985+
{
986+
// NOP
987+
}
988+
989+
/// <summary>
990+
/// Disposes all resources that were initialized by this locking model.
991+
/// </summary>
992+
public override void OnClose()
993+
{
994+
// NOP
995+
}
996+
}
997+
998+
/// <summary>
999+
/// Default locking model (when no locking model was configured)
1000+
/// </summary>
1001+
private static Type defaultLockingModelType = typeof(ExclusiveLock);
1002+
1003+
/// <summary>
1004+
/// Specify default locking model
1005+
/// </summary>
1006+
/// <typeparam name="TLockingModel">Type of LockingModel</typeparam>
1007+
public static void SetDefaultLockingModelType<TLockingModel>() where TLockingModel : LockingModelBase
1008+
{
1009+
defaultLockingModelType = typeof(TLockingModel);
1010+
}
1011+
8991012
#endregion Locking Models
9001013

9011014
#region Public Instance Constructors
@@ -1112,7 +1225,7 @@ public override void ActivateOptions()
11121225

11131226
if (m_lockingModel == null)
11141227
{
1115-
m_lockingModel = new FileAppender.ExclusiveLock();
1228+
m_lockingModel = (LockingModelBase)Activator.CreateInstance(defaultLockingModelType);
11161229
}
11171230

11181231
m_lockingModel.CurrentAppender = this;

0 commit comments

Comments
 (0)