From a4d877f60d48ab9bc27f42cc358f8c0a9d4f572f Mon Sep 17 00:00:00 2001 From: Jan Friedrich Date: Fri, 5 Apr 2024 23:09:16 +0200 Subject: [PATCH] #124 added links to https://github.com/dotnet/runtime - more pattern matching --- src/log4net/Appender/AdoNetAppender.cs | 12 ++--- src/log4net/Core/DefaultRepositorySelector.cs | 46 ++++++++----------- .../CodeAnalysis/AllowNullAttribute.cs | 2 + .../CallerArgumentExpressionAttribute.cs | 2 + .../CompilerFeatureRequiredAttribute.cs | 2 + .../CodeAnalysis/DisallowNullAttribute.cs | 2 + .../CodeAnalysis/DoesNotReturnAttribute.cs | 2 + .../CodeAnalysis/DoesNotReturnIfAttribute.cs | 2 + .../CodeAnalysis/IsExternalInit.cs | 2 + .../CodeAnalysis/MaybeNullAttribute.cs | 23 +++++++++- .../CodeAnalysis/MaybeNullWhenAttribute.cs | 23 +++++++++- .../CodeAnalysis/MemberNotNullAttribute.cs | 3 ++ .../MemberNotNullWhenAttribute.cs | 2 + .../CodeAnalysis/NotNullAttribute.cs | 2 + .../CodeAnalysis/NotNullIfNotNullAttribute.cs | 2 + .../CodeAnalysis/NotNullWhenAttribute.cs | 2 + .../CodeAnalysis/NullableAttribute.cs | 2 + .../CodeAnalysis/NullableContextAttribute.cs | 2 + .../CodeAnalysis/RequiredMemberAttribute.cs | 2 + .../SetsRequiredMembersAttribute.cs | 2 + .../Util/LogicalThreadContextProperties.cs | 10 ++-- 21 files changed, 102 insertions(+), 45 deletions(-) diff --git a/src/log4net/Appender/AdoNetAppender.cs b/src/log4net/Appender/AdoNetAppender.cs index e288d3b9..1e9333df 100644 --- a/src/log4net/Appender/AdoNetAppender.cs +++ b/src/log4net/Appender/AdoNetAppender.cs @@ -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 @@ -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 } @@ -581,7 +575,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con /// A connection string used to connect to the database. protected virtual string ResolveConnectionString(out string connectionStringContext) { - if (ConnectionString is string { Length: > 0 }) + if (ConnectionString is { Length: > 0 }) { connectionStringContext = "ConnectionString"; return ConnectionString; @@ -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); diff --git a/src/log4net/Core/DefaultRepositorySelector.cs b/src/log4net/Core/DefaultRepositorySelector.cs index 204a5c16..250d5a20 100644 --- a/src/log4net/Core/DefaultRepositorySelector.cs +++ b/src/log4net/Core/DefaultRepositorySelector.cs @@ -678,24 +678,19 @@ private static void ConfigureRepository(Assembly assembly, ILoggerRepository rep /// 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(); - 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); } } } @@ -712,24 +707,19 @@ private static void LoadPlugins(Assembly assembly, ILoggerRepository repository) /// 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(); - 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); } } } diff --git a/src/log4net/Diagnostics/CodeAnalysis/AllowNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/AllowNullAttribute.cs index 4d738601..5065cbc6 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/AllowNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/AllowNullAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/CallerArgumentExpressionAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/CallerArgumentExpressionAttribute.cs index 3533cfd5..27a446b0 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/CallerArgumentExpressionAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/CallerArgumentExpressionAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/CompilerFeatureRequiredAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/CompilerFeatureRequiredAttribute.cs index 7ccc60c1..a28a082b 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/CompilerFeatureRequiredAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/CompilerFeatureRequiredAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/DisallowNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/DisallowNullAttribute.cs index 4cd0bee3..85c944bd 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/DisallowNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/DisallowNullAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnAttribute.cs index 800a0eda..b2124837 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnIfAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnIfAttribute.cs index 520559b8..4e8d6f37 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnIfAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/DoesNotReturnIfAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/IsExternalInit.cs b/src/log4net/Diagnostics/CodeAnalysis/IsExternalInit.cs index 20f890f5..3fe56a63 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/IsExternalInit.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/IsExternalInit.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/MaybeNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/MaybeNullAttribute.cs index 974e0a1f..f3bd6a4f 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/MaybeNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/MaybeNullAttribute.cs @@ -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; /// diff --git a/src/log4net/Diagnostics/CodeAnalysis/MaybeNullWhenAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/MaybeNullWhenAttribute.cs index 6038ae9d..e90a062a 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/MaybeNullWhenAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/MaybeNullWhenAttribute.cs @@ -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; /// diff --git a/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullAttribute.cs index 728ed612..f7d7f950 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullWhenAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullWhenAttribute.cs index 6234bc3c..8f9f9e07 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullWhenAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/MemberNotNullWhenAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/NotNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/NotNullAttribute.cs index 7199d4b5..bebdfa6e 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/NotNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/NotNullAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/NotNullIfNotNullAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/NotNullIfNotNullAttribute.cs index 39860bcb..de3ffd81 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/NotNullIfNotNullAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/NotNullIfNotNullAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/NotNullWhenAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/NotNullWhenAttribute.cs index 86973727..c48b266a 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/NotNullWhenAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/NotNullWhenAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/NullableAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/NullableAttribute.cs index 0757cc06..0eacbdf5 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/NullableAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/NullableAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/NullableContextAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/NullableContextAttribute.cs index 0b16d806..9517b293 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/NullableContextAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/NullableContextAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/RequiredMemberAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/RequiredMemberAttribute.cs index a824e6b2..cc83b326 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/RequiredMemberAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/RequiredMemberAttribute.cs @@ -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; diff --git a/src/log4net/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs b/src/log4net/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs index d844d8fe..43cf352e 100644 --- a/src/log4net/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs +++ b/src/log4net/Diagnostics/CodeAnalysis/SetsRequiredMembersAttribute.cs @@ -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; diff --git a/src/log4net/Util/LogicalThreadContextProperties.cs b/src/log4net/Util/LogicalThreadContextProperties.cs index 5ec3b382..16fc376e 100644 --- a/src/log4net/Util/LogicalThreadContextProperties.cs +++ b/src/log4net/Util/LogicalThreadContextProperties.cs @@ -37,7 +37,7 @@ namespace log4net.Util /// /// /// This class stores its properties in a slot on the named - /// log4net.Util.LogicalThreadContextProperties for .net4x, + /// for .net4x, /// otherwise System.Threading.AsyncLocal /// /// @@ -99,10 +99,9 @@ public override object? this[string key] /// 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); } @@ -118,8 +117,7 @@ public void Remove(string key) /// public void Clear() { - PropertiesDictionary? dictionary = GetProperties(false); - if (dictionary is not null) + if (GetProperties(false) is not null) { SetLogicalProperties(new PropertiesDictionary()); }