diff --git a/Duplicati/Library/Localization/Duplicati.Library.Localization.csproj b/Duplicati/Library/Localization/Duplicati.Library.Localization.csproj index c0b7d85de..7e31378b3 100644 --- a/Duplicati/Library/Localization/Duplicati.Library.Localization.csproj +++ b/Duplicati/Library/Localization/Duplicati.Library.Localization.csproj @@ -10,4 +10,7 @@ + + + \ No newline at end of file diff --git a/Duplicati/Library/Localization/LocalizationContext.cs b/Duplicati/Library/Localization/LocalizationContext.cs index 872fa5250..5eba0d441 100644 --- a/Duplicati/Library/Localization/LocalizationContext.cs +++ b/Duplicati/Library/Localization/LocalizationContext.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2024, The Duplicati Team +// Copyright (C) 2024, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a @@ -19,7 +19,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +using Duplicati.Library.Logging; using System; + namespace Duplicati.Library.Localization { /// @@ -43,9 +45,8 @@ namespace Duplicati.Library.Localization /// The localization to use. public LocalizationContext(System.Globalization.CultureInfo ci) { - m_prev = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(LocalizationService.LOGICAL_CONTEXT_KEY); - System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LocalizationService.LOGICAL_CONTEXT_KEY, ci.Name); - m_isDisposed = false; + m_prev = CallContext.GetData(LocalizationService.LOGICAL_CONTEXT_KEY) as string; + CallContext.SetData(LocalizationService.LOGICAL_CONTEXT_KEY, ci.Name); m_isDisposed = false; } /// @@ -61,7 +62,7 @@ namespace Duplicati.Library.Localization { if (!m_isDisposed) { - System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LocalizationService.LOGICAL_CONTEXT_KEY, m_prev); + CallContext.SetData(LocalizationService.LOGICAL_CONTEXT_KEY, m_prev); m_isDisposed = true; } } diff --git a/Duplicati/Library/Localization/LocalizationService.cs b/Duplicati/Library/Localization/LocalizationService.cs index 85f2b4499..662f1b712 100644 --- a/Duplicati/Library/Localization/LocalizationService.cs +++ b/Duplicati/Library/Localization/LocalizationService.cs @@ -25,6 +25,7 @@ using System.Globalization; using System.Collections.Generic; using System.Text.RegularExpressions; using Duplicati.Library.Localization.Short; +using Duplicati.Library.Logging; namespace Duplicati.Library.Localization { @@ -97,7 +98,7 @@ namespace Duplicati.Library.Localization { get { - var lc = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(LOGICAL_CONTEXT_KEY) as string; + var lc = CallContext.GetData(LOGICAL_CONTEXT_KEY) as string; if (!string.IsNullOrWhiteSpace(lc)) return Get(new CultureInfo(lc)); return Get(CultureInfo.CurrentCulture); diff --git a/Duplicati/Library/Logging/CallContext.cs b/Duplicati/Library/Logging/CallContext.cs new file mode 100644 index 000000000..210cbf411 --- /dev/null +++ b/Duplicati/Library/Logging/CallContext.cs @@ -0,0 +1,30 @@ +using System.Collections.Concurrent; +using System.Threading; + +namespace Duplicati.Library.Logging +{ + /// + /// Provides a way to set contextual data that flows with the call and + /// async context of a test or invocation. + /// + public static class CallContext + { + static ConcurrentDictionary> state = new ConcurrentDictionary>(); + + /// + /// Stores a given object and associates it with the specified name. + /// + /// The name with which to associate the new item in the call context. + /// The object to store in the call context. + public static void SetData(string name, object data) => + state.GetOrAdd(name, _ => new AsyncLocal()).Value = data; + + /// + /// Retrieves an object with the specified name from the . + /// + /// The name of the item in the call context. + /// The object in the call context associated with the specified name, or if not found. + public static object GetData(string name) => + state.TryGetValue(name, out AsyncLocal data) ? data.Value : null; + } +} \ No newline at end of file diff --git a/Duplicati/Library/Logging/Log.cs b/Duplicati/Library/Logging/Log.cs index 7a80cf5d9..097822d62 100644 --- a/Duplicati/Library/Logging/Log.cs +++ b/Duplicati/Library/Logging/Log.cs @@ -445,7 +445,7 @@ namespace Duplicati.Library.Logging { lock (m_lock) { - var cur = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(LOGICAL_CONTEXT_KEY) as string; + var cur = CallContext.GetData(LOGICAL_CONTEXT_KEY) as string; if (cur == null || cur == m_root.InstanceID) return m_root; @@ -463,11 +463,11 @@ namespace Duplicati.Library.Logging if (value != null) { m_log_instances[value.InstanceID] = value; - System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LOGICAL_CONTEXT_KEY, value.InstanceID); + CallContext.SetData(LOGICAL_CONTEXT_KEY, value.InstanceID); } else { - System.Runtime.Remoting.Messaging.CallContext.LogicalSetData(LOGICAL_CONTEXT_KEY, null); + CallContext.SetData(LOGICAL_CONTEXT_KEY, null); } } } diff --git a/Duplicati/UnitTest/Duplicati.UnitTest.csproj b/Duplicati/UnitTest/Duplicati.UnitTest.csproj index 1ea1681e3..90dba1d95 100644 --- a/Duplicati/UnitTest/Duplicati.UnitTest.csproj +++ b/Duplicati/UnitTest/Duplicati.UnitTest.csproj @@ -8,7 +8,6 @@ -