Skip to content

Commit 96e736c

Browse files
authored
file scoped namespaces and CA warnings fixed in examples (#202)
1 parent f87956b commit 96e736c

30 files changed

+1879
-1763
lines changed

examples/Appenders/SampleAppendersApp/Appender/AsyncAppender.cs

+110-84
Original file line numberDiff line numberDiff line change
@@ -23,130 +23,156 @@
2323
using log4net.Core;
2424
using log4net.Util;
2525

26-
namespace SampleAppendersApp.Appender
26+
namespace SampleAppendersApp.Appender;
27+
28+
/// <summary>
29+
/// Appender that forwards LoggingEvents asynchronously
30+
/// </summary>
31+
/// <remarks>
32+
/// This appender forwards LoggingEvents to a list of attached appenders.
33+
/// The events are forwarded asynchronously using the ThreadPool.
34+
/// This allows the calling thread to be released quickly, however it does
35+
/// not guarantee the ordering of events delivered to the attached appenders.
36+
/// </remarks>
37+
public sealed class AsyncAppender : IAppender, IBulkAppender, IOptionHandler, IAppenderAttachable
2738
{
28-
/// <summary>
29-
/// Appender that forwards LoggingEvents asynchronously
30-
/// </summary>
31-
/// <remarks>
32-
/// This appender forwards LoggingEvents to a list of attached appenders.
33-
/// The events are forwarded asynchronously using the ThreadPool.
34-
/// This allows the calling thread to be released quickly, however it does
35-
/// not guarantee the ordering of events delivered to the attached appenders.
36-
/// </remarks>
37-
public sealed class AsyncAppender : IAppender, IBulkAppender, IOptionHandler, IAppenderAttachable
38-
{
39-
private readonly object syncRoot = new();
39+
private readonly object _syncRoot = new();
4040

41-
/// <inheritdoc/>
42-
public string Name { get; set; } = string.Empty;
41+
/// <inheritdoc/>
42+
public string Name { get; set; } = string.Empty;
4343

44-
/// <inheritdoc/>
45-
public void ActivateOptions()
46-
{ }
44+
/// <inheritdoc/>
45+
public void ActivateOptions()
46+
{ }
4747

48-
/// <inheritdoc/>
49-
public FixFlags Fix { get; set; } = FixFlags.All;
48+
/// <inheritdoc/>
49+
public FixFlags Fix { get; set; } = FixFlags.All;
5050

51-
/// <inheritdoc/>
52-
public void Close()
51+
/// <inheritdoc/>
52+
public void Close()
53+
{
54+
// Remove all the attached appenders
55+
lock (_syncRoot)
5356
{
54-
// Remove all the attached appenders
55-
lock (syncRoot)
56-
appenderAttachedImpl?.RemoveAllAppenders();
57+
_appenderAttachedImpl?.RemoveAllAppenders();
5758
}
59+
}
60+
61+
/// <inheritdoc/>
62+
public void DoAppend(LoggingEvent loggingEvent)
63+
{
64+
ArgumentNullException.ThrowIfNull(loggingEvent);
65+
loggingEvent.Fix = Fix;
66+
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncAppend), loggingEvent);
67+
}
5868

59-
/// <inheritdoc/>
60-
public void DoAppend(LoggingEvent loggingEvent)
69+
/// <inheritdoc/>
70+
public void DoAppend(LoggingEvent[] loggingEvents)
71+
{
72+
ArgumentNullException.ThrowIfNull(loggingEvents);
73+
foreach (LoggingEvent loggingEvent in loggingEvents)
6174
{
62-
ArgumentNullException.ThrowIfNull(loggingEvent);
6375
loggingEvent.Fix = Fix;
64-
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncAppend), loggingEvent);
6576
}
6677

67-
/// <inheritdoc/>
68-
public void DoAppend(LoggingEvent[] loggingEvents)
69-
{
70-
ArgumentNullException.ThrowIfNull(loggingEvents);
71-
foreach (LoggingEvent loggingEvent in loggingEvents)
72-
loggingEvent.Fix = Fix;
73-
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncAppend), loggingEvents);
74-
}
78+
ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncAppend), loggingEvents);
79+
}
7580

76-
private void AsyncAppend(object? state)
81+
private void AsyncAppend(object? state)
82+
{
83+
if (_appenderAttachedImpl != null)
7784
{
78-
if (appenderAttachedImpl != null)
85+
if (state is LoggingEvent loggingEvent)
86+
{
87+
_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvent);
88+
}
89+
else if (state is LoggingEvent[] loggingEvents)
7990
{
80-
if (state is LoggingEvent loggingEvent)
81-
appenderAttachedImpl.AppendLoopOnAppenders(loggingEvent);
82-
else if (state is LoggingEvent[] loggingEvents)
83-
appenderAttachedImpl.AppendLoopOnAppenders(loggingEvents);
91+
_appenderAttachedImpl.AppendLoopOnAppenders(loggingEvents);
8492
}
8593
}
94+
}
8695

87-
#region IAppenderAttachable Members
96+
#region IAppenderAttachable Members
8897

89-
/// <inheritdoc/>
90-
public void AddAppender(IAppender appender)
98+
/// <inheritdoc/>
99+
public void AddAppender(IAppender appender)
100+
{
101+
ArgumentNullException.ThrowIfNull(appender);
102+
lock (_syncRoot)
91103
{
92-
ArgumentNullException.ThrowIfNull(appender);
93-
lock (syncRoot)
94-
(appenderAttachedImpl ??= new()).AddAppender(appender);
104+
(_appenderAttachedImpl ??= new()).AddAppender(appender);
95105
}
106+
}
96107

97-
/// <inheritdoc/>
98-
public AppenderCollection Appenders
108+
/// <inheritdoc/>
109+
public AppenderCollection Appenders
110+
{
111+
get
99112
{
100-
get
113+
lock (_syncRoot)
101114
{
102-
lock (syncRoot)
103-
return appenderAttachedImpl?.Appenders ?? AppenderCollection.EmptyCollection;
115+
return _appenderAttachedImpl?.Appenders ?? AppenderCollection.EmptyCollection;
104116
}
105117
}
118+
}
106119

107-
/// <inheritdoc/>
108-
public IAppender? GetAppender(string name)
120+
/// <inheritdoc/>
121+
public IAppender? GetAppender(string? name)
122+
{
123+
lock (_syncRoot)
109124
{
110-
lock (syncRoot)
125+
if (_appenderAttachedImpl is null || name is null)
111126
{
112-
if (appenderAttachedImpl is null || name is null)
113-
return null;
114-
115-
return appenderAttachedImpl.GetAppender(name);
127+
return null;
116128
}
129+
130+
return _appenderAttachedImpl.GetAppender(name);
117131
}
132+
}
118133

119-
/// <inheritdoc/>
120-
public void RemoveAllAppenders()
134+
/// <inheritdoc/>
135+
public void RemoveAllAppenders()
136+
{
137+
lock (_syncRoot)
121138
{
122-
lock (syncRoot)
123-
if (appenderAttachedImpl is not null)
124-
{
125-
appenderAttachedImpl.RemoveAllAppenders();
126-
appenderAttachedImpl = null;
127-
}
139+
if (_appenderAttachedImpl is not null)
140+
{
141+
_appenderAttachedImpl.RemoveAllAppenders();
142+
_appenderAttachedImpl = null;
143+
}
128144
}
145+
}
129146

130-
/// <inheritdoc/>
131-
public IAppender? RemoveAppender(IAppender appender)
147+
/// <inheritdoc/>
148+
public IAppender? RemoveAppender(IAppender appender)
149+
{
150+
lock (_syncRoot)
132151
{
133-
lock (syncRoot)
134-
if (appender is not null && appenderAttachedImpl is not null)
135-
return appenderAttachedImpl.RemoveAppender(appender);
136-
return null;
152+
if (appender is not null && _appenderAttachedImpl is not null)
153+
{
154+
return _appenderAttachedImpl.RemoveAppender(appender);
155+
}
137156
}
138157

139-
/// <inheritdoc/>
140-
public IAppender? RemoveAppender(string name)
158+
return null;
159+
}
160+
161+
/// <inheritdoc/>
162+
public IAppender? RemoveAppender(string name)
163+
{
164+
lock (_syncRoot)
141165
{
142-
lock (syncRoot)
143-
if (name is not null && appenderAttachedImpl is not null)
144-
return appenderAttachedImpl.RemoveAppender(name);
145-
return null;
166+
if (name is not null && _appenderAttachedImpl is not null)
167+
{
168+
return _appenderAttachedImpl.RemoveAppender(name);
169+
}
146170
}
147171

148-
#endregion
149-
150-
private AppenderAttachedImpl? appenderAttachedImpl;
172+
return null;
151173
}
174+
175+
#endregion
176+
177+
private AppenderAttachedImpl? _appenderAttachedImpl;
152178
}

examples/Appenders/SampleAppendersApp/Appender/FireEventAppender.cs

+34-35
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,49 @@
2121

2222
using log4net.Core;
2323

24-
namespace SampleAppendersApp.Appender
24+
namespace SampleAppendersApp.Appender;
25+
26+
/// <inheritdoc/>
27+
public sealed class MessageLoggedEventArgs(LoggingEvent loggingEvent) : EventArgs
2528
{
2629
/// <inheritdoc/>
27-
public sealed class MessageLoggedEventArgs(LoggingEvent loggingEvent) : EventArgs
28-
{
29-
/// <inheritdoc/>
30-
public LoggingEvent LoggingEvent { get; } = loggingEvent;
31-
}
30+
public LoggingEvent LoggingEvent { get; } = loggingEvent;
31+
}
3232

33+
/// <summary>
34+
/// Appender that raises an event for each LoggingEvent received
35+
/// </summary>
36+
/// <remarks>
37+
/// Raises a MessageLoggedEvent for each LoggingEvent object received
38+
/// by this appender.
39+
/// </remarks>
40+
public class FireEventAppender : log4net.Appender.AppenderSkeleton
41+
{
3342
/// <summary>
34-
/// Appender that raises an event for each LoggingEvent received
43+
/// Event handler
3544
/// </summary>
36-
/// <remarks>
37-
/// Raises a MessageLoggedEvent for each LoggingEvent object received
38-
/// by this appender.
39-
/// </remarks>
40-
public class FireEventAppender : log4net.Appender.AppenderSkeleton
41-
{
42-
/// <summary>
43-
/// Event handler
44-
/// </summary>
45-
public event EventHandler<MessageLoggedEventArgs>? MessageLoggedEvent;
45+
public event EventHandler<MessageLoggedEventArgs>? MessageLoggedEvent;
4646

47-
/// <summary>
48-
/// Easy singleton, gets the last instance created
49-
/// </summary>
50-
public static FireEventAppender? Instance { get; private set; }
47+
/// <summary>
48+
/// Easy singleton, gets the last instance created
49+
/// </summary>
50+
public static FireEventAppender? Instance { get; private set; }
5151

52-
/// <inheritdoc/>
53-
public FireEventAppender() => Instance = this; // Store the instance created
52+
/// <inheritdoc/>
53+
public FireEventAppender() => Instance = this; // Store the instance created
5454

55-
/// <inheritdoc/>
56-
public virtual FixFlags Fix { get; set; } = FixFlags.All;
55+
/// <inheritdoc/>
56+
public virtual FixFlags Fix { get; set; } = FixFlags.All;
5757

58-
/// <inheritdoc/>
59-
protected override void Append(LoggingEvent loggingEvent)
60-
{
61-
ArgumentNullException.ThrowIfNull(loggingEvent);
62-
// Because we the LoggingEvent may be used beyond the lifetime
63-
// of the Append() method we must fix any volatile data in the event
64-
loggingEvent.Fix = Fix;
58+
/// <inheritdoc/>
59+
protected override void Append(LoggingEvent loggingEvent)
60+
{
61+
ArgumentNullException.ThrowIfNull(loggingEvent);
62+
// Because we the LoggingEvent may be used beyond the lifetime
63+
// of the Append() method we must fix any volatile data in the event
64+
loggingEvent.Fix = Fix;
6565

66-
// Raise the event
67-
MessageLoggedEvent?.Invoke(this, new MessageLoggedEventArgs(loggingEvent));
68-
}
66+
// Raise the event
67+
MessageLoggedEvent?.Invoke(this, new MessageLoggedEventArgs(loggingEvent));
6968
}
7069
}

0 commit comments

Comments
 (0)