Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- more pattern matching
  • Loading branch information
FreeAndNil committed Apr 5, 2024
1 parent f0b5ad4 commit a4d877f
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 45 deletions.
12 changes: 3 additions & 9 deletions src/log4net/Appender/AdoNetAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,13 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
{
if (!string.IsNullOrWhiteSpace(CommandText))
{
using IDbCommand dbCmd = Connection!.CreateCommand();
// Set the command string
using IDbCommand dbCmd = Connection.EnsureNotNull().CreateCommand();
dbCmd.CommandText = CommandText;

// Set the command type
dbCmd.CommandType = CommandType;
// Send buffer using the prepared command object
if (dbTran is not null)
{
dbCmd.Transaction = dbTran;
}

try
{
// prepare the command, which is significantly faster
Expand All @@ -476,7 +471,6 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
// rethrow exception in transaction mode, cuz now transaction is in failed state
throw;
}

// ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
}

Expand Down Expand Up @@ -581,7 +575,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con
/// <returns>A connection string used to connect to the database.</returns>
protected virtual string ResolveConnectionString(out string connectionStringContext)
{
if (ConnectionString is string { Length: > 0 })
if (ConnectionString is { Length: > 0 })
{
connectionStringContext = "ConnectionString";
return ConnectionString;
Expand All @@ -601,7 +595,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont
}
}

if (AppSettingsKey is string { Length: > 0 })
if (AppSettingsKey is { Length: > 0 })
{
connectionStringContext = "AppSettingsKey";
string? appSettingsConnectionString = SystemInfo.GetAppSetting(AppSettingsKey);
Expand Down
46 changes: 18 additions & 28 deletions src/log4net/Core/DefaultRepositorySelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,24 +678,19 @@ private static void ConfigureRepository(Assembly assembly, ILoggerRepository rep
/// </exception>
private static void LoadPlugins(Assembly assembly, ILoggerRepository repository)
{
repository.EnsureNotNull();

// Look for the PluginAttribute on the assembly
PluginAttribute[] configAttributes = Attribute.GetCustomAttributes(assembly.EnsureNotNull(), typeof(PluginAttribute), false)
PluginAttribute[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(PluginAttribute), false)
.EnsureIs<PluginAttribute[]>();
if (configAttributes.Length > 0)
foreach (PluginAttribute configAttr in configAttributes)
{
foreach (PluginAttribute configAttr in configAttributes)
try
{
try
{
// Create the plugin and add it to the repository
repository.PluginMap.Add(configAttr.CreatePlugin());
}
catch (Exception ex)
{
LogLog.Error(declaringType, $"Failed to create plugin. Attribute [{configAttr}]", ex);
}
// Create the plugin and add it to the repository
repository.PluginMap.Add(configAttr.CreatePlugin());
}
catch (Exception ex)
{
LogLog.Error(declaringType, $"Failed to create plugin. Attribute [{configAttr}]", ex);
}
}
}
Expand All @@ -712,24 +707,19 @@ private static void LoadPlugins(Assembly assembly, ILoggerRepository repository)
/// </exception>
private void LoadAliases(Assembly assembly, ILoggerRepository repository)
{
assembly.EnsureNotNull();
repository.EnsureNotNull();

// Look for the AliasRepositoryAttribute on the assembly
AliasRepositoryAttribute[] configAttributes = Attribute.GetCustomAttributes(assembly, typeof(AliasRepositoryAttribute), false)
AliasRepositoryAttribute[] configAttributes = Attribute
.GetCustomAttributes(assembly, typeof(AliasRepositoryAttribute), false)
.EnsureIs<AliasRepositoryAttribute[]>();
if (configAttributes.Length > 0)
foreach (AliasRepositoryAttribute configAttr in configAttributes)
{
foreach (AliasRepositoryAttribute configAttr in configAttributes)
try
{
try
{
AliasRepository(configAttr.Name, repository);
}
catch (Exception ex)
{
LogLog.Error(declaringType, $"Failed to alias repository [{configAttr.Name}]", ex);
}
AliasRepository(configAttr.Name, repository);
}
catch (Exception ex)
{
LogLog.Error(declaringType, $"Failed to alias repository [{configAttr.Name}]", ex);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/AllowNullAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CallerArgumentExpressionAttribute.cs

#if !NET6_0_OR_GREATER
namespace System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/CompilerFeatureRequiredAttribute.cs

#if !NET7_0_OR_GREATER
namespace System.Runtime.CompilerServices;

Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/DisallowNullAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/IsExternalInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsExternalInit.cs

#if !NET6_0_OR_GREATER
using System.ComponentModel;

Expand Down
23 changes: 22 additions & 1 deletion src/log4net/Diagnostics/CodeAnalysis/MaybeNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#if !NET6_0_OR_GREATER
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down
23 changes: 22 additions & 1 deletion src/log4net/Diagnostics/CodeAnalysis/MaybeNullWhenAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#if !NET6_0_OR_GREATER
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs


#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/NotNullAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/NotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs

#if !NET6_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
2 changes: 2 additions & 0 deletions src/log4net/Diagnostics/CodeAnalysis/NullableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/NullableAttribute.cs

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/NullableContextAttribute.cs

using System.Diagnostics.CodeAnalysis;

namespace System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RequiredMemberAttribute.cs

#if !NET7_0_OR_GREATER
namespace System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
//
#endregion

// inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs

#if !NET7_0_OR_GREATER
namespace System.Diagnostics.CodeAnalysis;

Expand Down
10 changes: 4 additions & 6 deletions src/log4net/Util/LogicalThreadContextProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace log4net.Util
/// </para>
/// <para>
/// This class stores its properties in a slot on the <see cref="CallContext"/> named
/// <c>log4net.Util.LogicalThreadContextProperties</c> for .net4x,
/// <see cref="log4net.Util.LogicalThreadContextProperties"/> for .net4x,
/// otherwise System.Threading.AsyncLocal
/// </para>
/// </remarks>
Expand Down Expand Up @@ -99,10 +99,9 @@ public override object? this[string key]
/// </remarks>
public void Remove(string key)
{
PropertiesDictionary? dictionary = GetProperties(false);
if (dictionary is not null)
if (GetProperties(false) is PropertiesDictionary dictionary)
{
PropertiesDictionary immutableProps = new PropertiesDictionary(dictionary);
PropertiesDictionary immutableProps = new(dictionary);
immutableProps.Remove(key);
SetLogicalProperties(immutableProps);
}
Expand All @@ -118,8 +117,7 @@ public void Remove(string key)
/// </remarks>
public void Clear()
{
PropertiesDictionary? dictionary = GetProperties(false);
if (dictionary is not null)
if (GetProperties(false) is not null)
{
SetLogicalProperties(new PropertiesDictionary());
}
Expand Down

0 comments on commit a4d877f

Please sign in to comment.