Hand-picked changes from #3124

This commit is contained in:
Kenneth Skovhede
2024-03-01 14:30:28 +01:00
parent aa20088c39
commit c168849dff
6 changed files with 44 additions and 10 deletions
@@ -10,4 +10,7 @@
<PackageReference Include="NGettext" Version="0.6.4" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Logging\Duplicati.Library.Logging.csproj" />
</ItemGroup>
</Project>
@@ -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
{
/// <summary>
@@ -43,9 +45,8 @@ namespace Duplicati.Library.Localization
/// <param name="ci">The localization to use.</param>
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;
}
/// <summary>
@@ -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;
}
}
@@ -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);
+30
View File
@@ -0,0 +1,30 @@
using System.Collections.Concurrent;
using System.Threading;
namespace Duplicati.Library.Logging
{
/// <summary>
/// Provides a way to set contextual data that flows with the call and
/// async context of a test or invocation.
/// </summary>
public static class CallContext
{
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
/// <summary>
/// Stores a given object and associates it with the specified name.
/// </summary>
/// <param name="name">The name with which to associate the new item in the call context.</param>
/// <param name="data">The object to store in the call context.</param>
public static void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
/// <summary>
/// Retrieves an object with the specified name from the <see cref="CallContext"/>.
/// </summary>
/// <param name="name">The name of the item in the call context.</param>
/// <returns>The object in the call context associated with the specified name, or <see langword="null"/> if not found.</returns>
public static object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
}
+3 -3
View File
@@ -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);
}
}
}
@@ -8,7 +8,6 @@
<ProjectReference Include="..\CommandLine\BackendTester\Duplicati.CommandLine.BackendTester.csproj" />
<ProjectReference Include="..\CommandLine\BackendTool\Duplicati.CommandLine.BackendTool.csproj" />
<ProjectReference Include="..\CommandLine\RecoveryTool\Duplicati.CommandLine.RecoveryTool.csproj" />
<ProjectReference Include="..\GUI\Duplicati.GUI.TrayIcon\Duplicati.GUI.TrayIcon.csproj" />
<ProjectReference Include="..\Library\AutoUpdater\Duplicati.Library.AutoUpdater.csproj" />
<ProjectReference Include="..\Library\Backend\AzureBlob\Duplicati.Library.Backend.AzureBlob.csproj" />
<ProjectReference Include="..\Library\Backend\CloudFiles\Duplicati.Library.Backend.CloudFiles.csproj" />