Skip to content

Commit

Permalink
Use Directory.build.props and LogEncryption property to configure log…
Browse files Browse the repository at this point in the history
…ging and enable demo base64 log encoding in release configuration
  • Loading branch information
TessenR committed Dec 2, 2020
1 parent 0652cf5 commit 02148b0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Directory.build.props
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<LogEncryption>true</LogEncryption>
</PropertyGroup>
</Project>
Expand Up @@ -13,6 +13,10 @@
<PackageReference Include="NLog" Version="4.7.4" />
</ItemGroup>

<ItemGroup>
<CompilerVisibleProperty Include="LogEncryption" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LoggingGenerator\LoggingGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Expand Down
13 changes: 11 additions & 2 deletions LoggingGenerator/LoggingProxyGenerator.cs
Expand Up @@ -33,6 +33,10 @@ public void Execute(GeneratorExecutionContext context)

var compilation = context.Compilation;

context.AnalyzerConfigOptions.GlobalOptions
.TryGetValue("build_property.LogEncryption", out var logEncryptionStr);
bool.TryParse(logEncryptionStr, out var encryptLog);

var syntaxReceiver = (SyntaxReceiver) context.SyntaxReceiver;
var loggingTargets = syntaxReceiver.TypeDeclarationsWithAttributes;

Expand Down Expand Up @@ -79,12 +83,12 @@ public void Execute(GeneratorExecutionContext context)
{
context.CancellationToken.ThrowIfCancellationRequested();

var proxySource = GenerateProxy(targetType, namespaceName);
var proxySource = GenerateProxy(targetType, namespaceName, encryptLog);
context.AddSource($"{targetType.Name}.Logging.cs", proxySource);
}
}

private string GenerateProxy(ITypeSymbol targetType, string namespaceName)
private string GenerateProxy(ITypeSymbol targetType, string namespaceName, bool encrypt)
{
var allInterfaceMethods = targetType.AllInterfaces
.SelectMany(x => x.GetMembers())
Expand Down Expand Up @@ -167,6 +171,11 @@ public class {proxyName} : {fullQualifiedName}

string Log(string logLevel, string message)
{
if (encrypt)
{
message = $"System.Convert.ToBase64String(Encoding.UTF8.GetBytes({message}))";
}

return $" _logger.Log({logLevel}, {message});";
}
}
Expand Down

0 comments on commit 02148b0

Please sign in to comment.