Skip to content

Commit c0d83ec

Browse files
authored
Merge pull request #134 from microsoft/users/jmyers/buildAndLog
Build fixes and logging improvements, Includes some build changes from @zarenner
2 parents 3d85b66 + fdb90b9 commit c0d83ec

File tree

18 files changed

+1287
-1177
lines changed

18 files changed

+1287
-1177
lines changed

CredentialProvider.Microsoft.Tests/CredentialProvider.Microsoft.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net461</TargetFramework>
4+
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Moq" Version="4.8.2" />
1313
<PackageReference Include="MSTest.TestAdapter" Version="1.2.1" />
1414
<PackageReference Include="MSTest.TestFramework" Version="1.2.1" />
15+
<PackageReference Include="NuGet.Protocol" Version="5.3.0" />
1516
</ItemGroup>
1617

1718
<ItemGroup>

CredentialProvider.Microsoft.Tests/Logging/LoggingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public class LoggingTests
1515
public void HumanFriendlyTextWriterLogger_EmitsLogLevelAndMessage()
1616
{
1717
mockWriter.Setup(x => x.WriteLine(It.IsAny<string>()));
18-
HumanFriendlyTextWriterLogger logger = new HumanFriendlyTextWriterLogger(mockWriter.Object);
18+
HumanFriendlyTextWriterLogger logger = new HumanFriendlyTextWriterLogger(mockWriter.Object, writesToConsole: false);
1919
logger.SetLogLevel(LogLevel.Error);
20-
logger.Log(LogLevel.Error, "Something bad happened");
20+
logger.Log(LogLevel.Error, allowOnConsole: true, "Something bad happened");
2121
mockWriter.Verify(x => x.WriteLine("[Error] [CredentialProvider]Something bad happened"));
2222
}
2323
}

CredentialProvider.Microsoft.VSIX/Microsoft.CredentialProvider.swixproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<ItemGroup>
2727
<SigningTarget Include="$(OutDir)$(OutputName)"/>
2828
<FilesToSign Include="@(SigningTarget)">
29-
<Authenticode>Vsix</Authenticode>
29+
<Authenticode>VsixSHA2</Authenticode>
3030
</FilesToSign>
3131
</ItemGroup>
3232

CredentialProvider.Microsoft.VSIX/Microsoft.CredentialProvider.swr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ folder InstallDir:\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\Credenti
1515
file source=$(PluginBinPath)\NuGet.Common.dll
1616
file source=$(PluginBinPath)\NuGet.Configuration.dll
1717
file source=$(PluginBinPath)\NuGet.Frameworks.dll
18-
file source=$(PluginBinPath)\NuGet.Packaging.Core.dll
1918
file source=$(PluginBinPath)\NuGet.Packaging.dll
2019
file source=$(PluginBinPath)\NuGet.Protocol.dll
2120
file source=$(PluginBinPath)\NuGet.Versioning.dll
@@ -94,6 +93,7 @@ folder InstallDir:\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\Credenti
9493
file source=$(PluginBinPath)\System.Security.Cryptography.Csp.dll
9594
file source=$(PluginBinPath)\System.Security.Cryptography.Encoding.dll
9695
file source=$(PluginBinPath)\System.Security.Cryptography.Primitives.dll
96+
file source=$(PluginBinPath)\System.Security.Cryptography.ProtectedData.dll
9797
file source=$(PluginBinPath)\System.Security.Cryptography.X509Certificates.dll
9898
file source=$(PluginBinPath)\System.Security.Principal.dll
9999
file source=$(PluginBinPath)\System.Security.SecureString.dll

CredentialProvider.Microsoft/CredentialProvider.Microsoft.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<ItemGroup>
3535
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.19.4" />
36-
<PackageReference Include="NuGet.Protocol" Version="5.2.0" />
36+
<PackageReference Include="NuGet.Protocol" Version="5.3.0" />
3737
<PackageReference Include="PowerArgs" Version="3.0.0" />
3838
</ItemGroup>
3939

@@ -65,11 +65,11 @@
6565

6666
<ItemGroup>
6767
<FilesToSign Include="$(OutDir)/$(TargetFileName)">
68-
<Authenticode>Microsoft</Authenticode>
68+
<Authenticode>Microsoft400</Authenticode>
6969
<StrongName>StrongName</StrongName>
7070
</FilesToSign>
7171
<FilesToSign Include="$(IntermediateOutputPath)/$(TargetFileName)">
72-
<Authenticode>Microsoft</Authenticode>
72+
<Authenticode>Microsoft400</Authenticode>
7373
<StrongName>StrongName</StrongName>
7474
</FilesToSign>
7575
</ItemGroup>

CredentialProvider.Microsoft/Logging/ConsoleLoggers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace NuGetCredentialProvider.Logging
1313
/// </summary>
1414
public class StandardOutputLogger : HumanFriendlyTextWriterLogger
1515
{
16-
public StandardOutputLogger() : base(Console.Out) { }
16+
public StandardOutputLogger() : base(Console.Out, writesToConsole: true) { }
1717
}
1818

1919
/// <summary>
2020
/// Logs messages to standard error, with log level included
2121
/// </summary>
2222
public class StandardErrorLogger : HumanFriendlyTextWriterLogger
2323
{
24-
public StandardErrorLogger() : base(Console.Error) { }
24+
public StandardErrorLogger() : base(Console.Error, writesToConsole: true) { }
2525
}
2626

2727
/// <summary>
@@ -31,7 +31,8 @@ public class HumanFriendlyTextWriterLogger : LoggerBase
3131
{
3232
private readonly TextWriter writer;
3333

34-
public HumanFriendlyTextWriterLogger(TextWriter writer)
34+
public HumanFriendlyTextWriterLogger(TextWriter writer, bool writesToConsole)
35+
: base(writesToConsole)
3536
{
3637
this.writer = writer;
3738
}

CredentialProvider.Microsoft/Logging/FileLogger.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

CredentialProvider.Microsoft/Logging/ILogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NuGetCredentialProvider.Logging
88
{
99
public interface ILogger
1010
{
11-
void Log(LogLevel level, string message);
11+
void Log(LogLevel level, bool allowOnConsole, string message);
1212

1313
void SetLogLevel(LogLevel newLogLevel);
1414
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
//
3+
// Licensed under the MIT license.
4+
5+
using System;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Threading;
9+
using NuGet.Common;
10+
11+
namespace NuGetCredentialProvider.Logging
12+
{
13+
internal class LogEveryMessageFileLogger : ILogger
14+
{
15+
private static readonly int Pid = Process.GetCurrentProcess().Id;
16+
17+
private readonly string filePath;
18+
19+
internal LogEveryMessageFileLogger(string filePath)
20+
{
21+
this.filePath = filePath;
22+
Log(LogLevel.Minimal, allowOnConsole: false, string.Format(Resources.LogStartsAt, DateTime.UtcNow.ToString("u")));
23+
}
24+
25+
public void Log(LogLevel level, bool allowOnConsole, string message)
26+
{
27+
try
28+
{
29+
for (int i = 0; i < 3; ++i)
30+
{
31+
try
32+
{
33+
File.AppendAllText(
34+
filePath,
35+
$"[{DateTime.UtcNow:HH:mm:ss.fff} {Pid,5} {GetLevelKeyword(level),7}] {message}\n");
36+
return;
37+
}
38+
catch (IOException)
39+
{
40+
// retry IOExceptions a couple of times. Could be another instance (or thread) of the plugin locking the file
41+
Thread.Sleep(TimeSpan.FromMilliseconds(20));
42+
}
43+
}
44+
}
45+
catch
46+
{
47+
// don't crash the process just because logging failed.
48+
}
49+
}
50+
51+
private string GetLevelKeyword(LogLevel level)
52+
{
53+
switch (level)
54+
{
55+
case LogLevel.Debug:
56+
return "Debug";
57+
58+
case LogLevel.Verbose:
59+
return "Verbose";
60+
61+
case LogLevel.Information:
62+
return "Info";
63+
64+
case LogLevel.Minimal:
65+
return "Minimal";
66+
67+
case LogLevel.Warning:
68+
return "Warning";
69+
70+
case LogLevel.Error:
71+
return "Error";
72+
73+
default:
74+
throw new ArgumentOutOfRangeException(nameof(level), level, null);
75+
}
76+
}
77+
78+
void ILogger.SetLogLevel(LogLevel newLogLevel)
79+
{
80+
// no-op. This logger always logs all messages.
81+
}
82+
}
83+
}

CredentialProvider.Microsoft/Logging/LoggerBase.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ public abstract class LoggerBase : ILogger
1414
private LogLevel minLogLevel = LogLevel.Debug;
1515
private bool allowLogWrites = false;
1616
private ConcurrentQueue<Tuple<LogLevel, string, DateTime>> bufferedLogs = new ConcurrentQueue<Tuple<LogLevel, string, DateTime>>();
17+
private readonly bool writesToConsole;
1718

18-
public void Log(LogLevel level, string message)
19+
protected LoggerBase(bool writesToConsole)
1920
{
21+
this.writesToConsole = writesToConsole;
22+
}
23+
24+
public void Log(LogLevel level, bool allowOnConsole, string message)
25+
{
26+
if (writesToConsole && !allowOnConsole)
27+
{
28+
return;
29+
}
30+
2031
if (!allowLogWrites)
2132
{
2233
// cheap reserve, if it swaps out after we add, meh, we miss one log

0 commit comments

Comments
 (0)