Skip to content

Commit 958bb47

Browse files
committed
HACK for TraceSwitch, since .Net core doesnt care.
1 parent 43d5a43 commit 958bb47

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/Org.Security.Cryptography.X509Extensions/MyTrace.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ internal static class MyTrace
1111
// Trace switch name: Org.Security.Cryptography
1212
static readonly string MyName = typeof(MyTrace).Namespace;
1313

14-
// My trace switch.
15-
static readonly TraceSwitch MyTraceSwitch = new TraceSwitch(MyName, MyName, $"{TraceLevel.Warning}");
14+
// Making TraceSwitch update-able, because of the .Net core MESS (I meant, one of the .Net core messes)
15+
// .Net core applications WILL NOT honor TraceSwitch in config files.
16+
// Use X509Extensions.TraceLevel property
17+
internal static TraceSwitch MyTraceSwitch { get; set; } = new TraceSwitch(MyName, MyName, $"{TraceLevel.Warning}");
1618

1719
[Conditional("DEBUG")]
1820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -60,6 +62,7 @@ internal static void Error(Exception err)
6062
{
6163
buffer.AppendLine(err.Message);
6264
buffer.AppendLine(err.GetType().FullName);
65+
err = err.InnerException;
6366
}
6467

6568
// Stack trac of the top level error.

src/Org.Security.Cryptography.X509Extensions/X509Extensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Security.Cryptography;
56
using System.Security.Cryptography.X509Certificates;
@@ -17,6 +18,18 @@ namespace Org.Security.Cryptography
1718
/// </summary>
1819
public static class X509Extensions
1920
{
21+
//...............................................................................
22+
#region About TripleDESCryptoServiceProvider, Microsoft says:
23+
//...............................................................................
24+
// A newer symmetric encryption algorithm, Advanced Encryption Standard (AES), is available.
25+
// Consider using the Aes class and its derived classes instead of the TripleDES class.
26+
// Use TripleDES only for compatibility with legacy applications and data.
27+
// const string DefaultDataEncryptionAlgorithm = "3DES";
28+
// const int DefaultDataEncryptionKeySize = 128;
29+
// const int DefaultDataEncryptionBlockSize = 64;
30+
31+
#endregion
32+
2033
const string DefaultDataEncryptionAlgorithm = "Aes";
2134
const int DefaultDataEncryptionKeySize = 256;
2235
const int DefaultDataEncryptionBlockSize = 128;
@@ -221,6 +234,18 @@ static Int32 ReadInt32(this Stream inputStream)
221234

222235
return BitConverter.ToInt32(fourBytes, startIndex: 0);
223236
}
237+
238+
/// <summary>
239+
/// Because, .Net core doesn't honor TraceSwitches from the config files.
240+
/// Try TraceSwitch "Org.Security.Cryptography" in config files.
241+
/// If it doesn't work, update me.
242+
/// </summary>
243+
public static TraceLevel TraceLevel
244+
{
245+
set {
246+
MyTrace.MyTraceSwitch = new TraceSwitch("", "", value.ToString());
247+
}
248+
}
224249
}
225250
}
226251

src/UnitTests/UnitTests.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,19 @@
1717
<ProjectReference Include="..\Org.Security.Cryptography.X509Extensions\Org.Security.Cryptography.X509Extensions.csproj" />
1818
</ItemGroup>
1919

20+
<!--
21+
<HACK>
22+
The entry point of .Net core Test project is in testhost.dll
23+
App.config is expected as testhost.dll.config NOT as UniTests.exe|dll.config
24+
Copy the (ProjectName).dll.config to testhost.dll.config
25+
If below target is removed, entries in App.config will not take effect.
26+
</HACK>
27+
<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build">
28+
<CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
29+
<Output TaskParameter="Include" ItemName="FilesToCopy" />
30+
</CreateItem>
31+
<Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
32+
</Target>
33+
-->
34+
2035
</Project>

src/UnitTests/X509Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public class X509Tests
2020

2121
const string CertThumbPrint = "2E3257EE8FC8A72DB3778DFB3F9EDC7D0A9D66C7";
2222

23+
[TestInitialize]
24+
public void TestInitialize()
25+
{
26+
X509Extensions.TraceLevel = TraceLevel.Info;
27+
}
28+
29+
2330
[TestMethod]
2431
public void FindCertificateTest()
2532
{

0 commit comments

Comments
 (0)