Skip to content

Commit

Permalink
#111 Updated examples for 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Sep 15, 2024
1 parent e614e1c commit 52bdba8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 33 deletions.
10 changes: 7 additions & 3 deletions examples/Appenders/WmiAppender/WmiAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public sealed class WmiAppender : IAppender, IOptionHandler
/// The name uniquely identifies the appender.
/// </para>
/// </remarks>
public string? Name { get; set; }
public string Name { get; set; } = default!;

/// <summary>
/// Gets or sets the threshold <see cref="Level"/> of this appender.
Expand Down Expand Up @@ -165,12 +165,16 @@ public void Close()
public void DoAppend(LoggingEvent loggingEvent)
{
if (loggingEvent is null)
{
throw new ArgumentNullException(nameof(loggingEvent));
}

try
{
if (IsAsSevereAsThreshold(loggingEvent.Level))
{
(Layout?.Format(loggingEvent))?.Fire();
}
}
catch (Exception ex)
{
Expand All @@ -179,8 +183,8 @@ public void DoAppend(LoggingEvent loggingEvent)
}

/// <summary>
/// Checks if the message level is below this appender's threshold.
/// Checks if the message level is below this appenders threshold.
/// </summary>
private bool IsAsSevereAsThreshold(Level level) => ((Threshold == null) || level >= Threshold);
private bool IsAsSevereAsThreshold(Level? level) => ((Threshold is null) || level >= Threshold);
}
}
2 changes: 1 addition & 1 deletion examples/Appenders/WmiAppender/WmiLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected virtual IEvent CreateEvent(LoggingEvent loggingEvent)
{
TimeStamp = loggingEvent.TimeStamp,
LoggerName = loggingEvent.LoggerName,
Level = loggingEvent.Level.DisplayName,
Level = (loggingEvent.Level ??Level.Debug).DisplayName,
Message = loggingEvent.RenderedMessage,
ThreadName = loggingEvent.ThreadName,
ExceptionString = loggingEvent.GetExceptionString(),
Expand Down
2 changes: 1 addition & 1 deletion examples/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup>
<Log4NetPackageVersion>2.0.17</Log4NetPackageVersion>
<Log4NetPackageVersion>3.0.0</Log4NetPackageVersion>
</PropertyGroup>
<PropertyGroup Label="GenerateAssemblyInfo">
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
Expand Down
3 changes: 2 additions & 1 deletion examples/Extensibility/EventIDLogApp/EventIDLogApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace EventIDLogApp
internal static class EventIDLogApp
{
// Create a logger for use in this class
private static readonly IEventIDLog log = EventIDLogManager.GetLogger(typeof(EventIDLogApp));
private static readonly IEventIDLog log = EventIDLogManager.GetLogger(typeof(EventIDLogApp))
?? throw new ArgumentNullException(nameof(EventIDLogManager.GetLogger));

/// <summary>
/// The main entry point for the application.
Expand Down
3 changes: 2 additions & 1 deletion examples/Extensibility/TraceLogApp/TraceLogApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace TraceLogApp
internal static class TraceLogApp
{
// Create a logger for use in this class
private static readonly ITraceLog log = TraceLogManager.GetLogger(typeof(TraceLogApp));
private static readonly ITraceLog log = TraceLogManager.GetLogger(typeof(TraceLogApp))
?? throw new ArgumentNullException(nameof(TraceLogManager.GetLogger));

/// <summary>
/// The main entry point for the application.
Expand Down
29 changes: 15 additions & 14 deletions examples/Extensions/log4net.Ext.EventID/EventIDLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using log4net.Core;

Expand Down Expand Up @@ -58,7 +59,7 @@ private EventIDLogManager() { }
/// </remarks>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static IEventIDLog Exists(string name) => Exists(Assembly.GetCallingAssembly(), name);
public static IEventIDLog? Exists(string name) => Exists(Assembly.GetCallingAssembly(), name);

/// <summary>
/// Returns the named logger if it exists
Expand All @@ -71,7 +72,7 @@ private EventIDLogManager() { }
/// <param name="domain">the domain to lookup in</param>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static IEventIDLog Exists(string domain, string name)
public static IEventIDLog? Exists(string domain, string name)
=> WrapLogger(LoggerManager.Exists(domain, name));

/// <summary>
Expand All @@ -85,7 +86,7 @@ public static IEventIDLog Exists(string domain, string name)
/// <param name="assembly">the assembly to use to lookup the domain</param>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static IEventIDLog Exists(Assembly assembly, string name)
public static IEventIDLog? Exists(Assembly assembly, string name)
=> WrapLogger(LoggerManager.Exists(assembly, name));

/// <summary>
Expand Down Expand Up @@ -135,7 +136,7 @@ public static IEventIDLog[] GetCurrentLoggers(Assembly assembly)
/// </remarks>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(string name)
public static IEventIDLog? GetLogger(string name)
=> GetLogger(Assembly.GetCallingAssembly(), name);

/// <summary>
Expand All @@ -154,7 +155,7 @@ public static IEventIDLog GetLogger(string name)
/// <param name="domain">the domain to lookup in</param>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(string domain, string name)
public static IEventIDLog? GetLogger(string domain, string name)
=> WrapLogger(LoggerManager.GetLogger(domain, name));

/// <summary>
Expand All @@ -173,7 +174,7 @@ public static IEventIDLog GetLogger(string domain, string name)
/// <param name="assembly">the assembly to use to lookup the domain</param>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(Assembly assembly, string name)
public static IEventIDLog? GetLogger(Assembly assembly, string name)
=> WrapLogger(LoggerManager.GetLogger(assembly, name));

/// <summary>
Expand All @@ -185,7 +186,7 @@ public static IEventIDLog GetLogger(Assembly assembly, string name)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(Type type)
public static IEventIDLog? GetLogger(Type type)
{
ArgumentNullException.ThrowIfNull(type);
return GetLogger(Assembly.GetCallingAssembly(), type.FullName ?? string.Empty);
Expand All @@ -201,7 +202,7 @@ public static IEventIDLog GetLogger(Type type)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(string domain, Type type)
public static IEventIDLog? GetLogger(string domain, Type type)
=> WrapLogger(LoggerManager.GetLogger(domain, type));

/// <summary>
Expand All @@ -214,7 +215,7 @@ public static IEventIDLog GetLogger(string domain, Type type)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static IEventIDLog GetLogger(Assembly assembly, Type type)
public static IEventIDLog? GetLogger(Assembly assembly, Type type)
=> WrapLogger(LoggerManager.GetLogger(assembly, type));

#endregion
Expand All @@ -226,19 +227,19 @@ public static IEventIDLog GetLogger(Assembly assembly, Type type)
/// </summary>
/// <param name="logger">the logger to get the wrapper for</param>
/// <returns>the wrapper for the logger specified</returns>
private static IEventIDLog WrapLogger(ILogger logger) => (IEventIDLog)wrapperMap.GetWrapper(logger);
private static IEventIDLog? WrapLogger(ILogger? logger) => (IEventIDLog?)wrapperMap.GetWrapper(logger);

/// <summary>
/// Lookup the wrapper objects for the loggers specified
/// </summary>
/// <param name="loggers">the loggers to get the wrappers for</param>
/// <returns>Lookup the wrapper objects for the loggers specified</returns>
private static IEventIDLog[] WrapLoggers(ILogger[] loggers)
private static IEventIDLog[] WrapLoggers(IReadOnlyList<ILogger> loggers)
{
IEventIDLog[] results = new IEventIDLog[loggers.Length];
for (int i = 0; i < loggers.Length; i++)
IEventIDLog[] results = new IEventIDLog[loggers.Count];
for (int i = 0; i < loggers.Count; i++)
{
results[i] = WrapLogger(loggers[i]);
results[i] = WrapLogger(loggers[i]) ?? throw new ArgumentNullException(nameof(loggers));
}
return results;
}
Expand Down
25 changes: 14 additions & 11 deletions examples/Extensions/log4net.Ext.Trace/TraceLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static class TraceLogManager
/// </remarks>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static ITraceLog Exists(string name) => Exists(Assembly.GetCallingAssembly(), name);
public static ITraceLog? Exists(string name) => Exists(Assembly.GetCallingAssembly(), name);

/// <summary>
/// Returns the named logger if it exists
Expand All @@ -62,7 +62,7 @@ public static class TraceLogManager
/// <param name="domain">the domain to lookup in</param>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static ITraceLog Exists(string domain, string name)
public static ITraceLog? Exists(string domain, string name)
=> WrapLogger(LoggerManager.Exists(domain, name));

/// <summary>
Expand All @@ -76,7 +76,7 @@ public static ITraceLog Exists(string domain, string name)
/// <param name="assembly">the assembly to use to lookup the domain</param>
/// <param name="name">The fully qualified logger name to look for</param>
/// <returns>The logger found, or null</returns>
public static ITraceLog Exists(Assembly assembly, string name)
public static ITraceLog? Exists(Assembly assembly, string name)
=> WrapLogger(LoggerManager.Exists(assembly, name));

/// <summary>
Expand Down Expand Up @@ -125,7 +125,7 @@ public static ITraceLog[] GetCurrentLoggers(Assembly assembly)
/// </remarks>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(string name) => GetLogger(Assembly.GetCallingAssembly(), name);
public static ITraceLog? GetLogger(string name) => GetLogger(Assembly.GetCallingAssembly(), name);

/// <summary>
/// Retrieve or create a named logger.
Expand All @@ -143,7 +143,7 @@ public static ITraceLog[] GetCurrentLoggers(Assembly assembly)
/// <param name="domain">the domain to lookup in</param>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(string domain, string name)
public static ITraceLog? GetLogger(string domain, string name)
=> WrapLogger(LoggerManager.GetLogger(domain, name));

/// <summary>
Expand All @@ -162,7 +162,7 @@ public static ITraceLog GetLogger(string domain, string name)
/// <param name="assembly">the assembly to use to lookup the domain</param>
/// <param name="name">The name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(Assembly assembly, string name)
public static ITraceLog? GetLogger(Assembly assembly, string name)
=> WrapLogger(LoggerManager.GetLogger(assembly, name));

/// <summary>
Expand All @@ -174,7 +174,7 @@ public static ITraceLog GetLogger(Assembly assembly, string name)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(Type type)
public static ITraceLog? GetLogger(Type type)
{
ArgumentNullException.ThrowIfNull(type?.FullName);
return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
Expand All @@ -190,7 +190,7 @@ public static ITraceLog GetLogger(Type type)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(string domain, Type type)
public static ITraceLog? GetLogger(string domain, Type type)
=> WrapLogger(LoggerManager.GetLogger(domain, type));

/// <summary>
Expand All @@ -203,7 +203,7 @@ public static ITraceLog GetLogger(string domain, Type type)
/// <param name="type">The full name of <paramref name="type"/> will
/// be used as the name of the logger to retrieve.</param>
/// <returns>the logger with the name specified</returns>
public static ITraceLog GetLogger(Assembly assembly, Type type)
public static ITraceLog? GetLogger(Assembly assembly, Type type)
=> WrapLogger(LoggerManager.GetLogger(assembly, type));

#endregion
Expand All @@ -215,7 +215,7 @@ public static ITraceLog GetLogger(Assembly assembly, Type type)
/// </summary>
/// <param name="logger">the logger to get the wrapper for</param>
/// <returns>the wrapper for the logger specified</returns>
private static ITraceLog WrapLogger(ILogger logger) => (ITraceLog)s_wrapperMap.GetWrapper(logger);
private static ITraceLog? WrapLogger(ILogger? logger) => (ITraceLog?)s_wrapperMap.GetWrapper(logger);

/// <summary>
/// Lookup the wrapper objects for the loggers specified
Expand All @@ -226,7 +226,10 @@ private static ITraceLog[] WrapLoggers(ILogger[] loggers)
{
ITraceLog[] results = new ITraceLog[loggers.Length];
for (int i = 0; i < loggers.Length; i++)
results[i] = WrapLogger(loggers[i]);
{
results[i] = WrapLogger(loggers[i]) ?? throw new ArgumentNullException(nameof(loggers));
}

return results;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5886EE0F-0A11-48A5-8618-75112878F381}"
ProjectSection(SolutionItems) = preProject
..\..\Directory.Build.props = ..\..\Directory.Build.props
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "Tutorials\ConsoleApp\ConsoleApp.csproj", "{988F2EF8-7CD9-4F78-A8C3-FBFBCE7CC5D2}"
Expand Down

0 comments on commit 52bdba8

Please sign in to comment.