Skip to content

Commit

Permalink
Merge pull request #445 from nblumhardt/dotnet-9-build-fixes
Browse files Browse the repository at this point in the history
Try to get `TestApp` running happily in the new build configuration
  • Loading branch information
nblumhardt authored Nov 23, 2024
2 parents 86fed08 + bc92b24 commit 120d753
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
2 changes: 2 additions & 0 deletions test/TestApp-net9.0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FodyWeavers.xml
FodyWeavers.xsd
70 changes: 70 additions & 0 deletions test/TestApp-net9.0/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Debugging;
using Serilog.Settings.Configuration;

if (args.Length == 1 && args[0] == "is-single-file")
{
if (typeof(Program).Assembly.GetManifestResourceNames().Any(e => e.StartsWith("costura.")))
{
Console.WriteLine(true);
return 0;
}
// IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app
#pragma warning disable IL3000
Console.WriteLine(string.IsNullOrEmpty(Assembly.GetEntryAssembly()?.Location));
#pragma warning restore
return 0;
}

SelfLog.Enable(Console.Error);

Thread.CurrentThread.Name = "Main thread";
const string outputTemplate = "({ThreadName}) [{Level}] {Message}{NewLine}";

var configurationValues = new Dictionary<string, string?>();
var minimumLevelOnly = args.Contains("--minimum-level-only");
if (minimumLevelOnly)
{
configurationValues["Serilog:MinimumLevel"] = "Verbose";
}
else
{
configurationValues["Serilog:Enrich:0"] = "WithThreadName";
configurationValues["Serilog:WriteTo:0:Name"] = "Console";
configurationValues["Serilog:WriteTo:0:Args:outputTemplate"] = outputTemplate;
}

if (args.Contains("--using-thread")) configurationValues["Serilog:Using:Thread"] = "Serilog.Enrichers.Thread";
if (args.Contains("--using-console")) configurationValues["Serilog:Using:Console"] = "Serilog.Sinks.Console";

var assemblies = new List<Assembly>();
if (args.Contains("--assembly-thread")) assemblies.Add(typeof(ThreadLoggerConfigurationExtensions).Assembly);
if (args.Contains("--assembly-console")) assemblies.Add(typeof(ConsoleLoggerConfigurationExtensions).Assembly);

try
{
var configuration = new ConfigurationBuilder().AddInMemoryCollection(configurationValues).Build();
var options = assemblies.Count > 0 ? new ConfigurationReaderOptions(assemblies.ToArray()) : null;
var loggerConfiguration = new LoggerConfiguration().ReadFrom.Configuration(configuration, options);
if (minimumLevelOnly)
{
loggerConfiguration
.Enrich.WithThreadName()
.WriteTo.Console(outputTemplate: outputTemplate);
}
var logger = loggerConfiguration.CreateLogger();
logger.Information("Expected success");
return 0;
}
catch (InvalidOperationException exception) when (exception.Message.StartsWith("No Serilog:Using configuration section is defined and no Serilog assemblies were found."))
{
Console.WriteLine("Expected exception");
return 0;
}
catch (Exception exception)
{
Console.Error.WriteLine(exception);
return 1;
}
Binary file not shown.
33 changes: 33 additions & 0 deletions test/TestApp-net9.0/TestApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
<DebugType>embedded</DebugType>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<GenerateSupportedRuntime>false</GenerateSupportedRuntime>
<PublishReferencesDocumentationFiles>false</PublishReferencesDocumentationFiles>
<AllowedReferenceRelatedFileExtensions>none</AllowedReferenceRelatedFileExtensions>
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
<SelfContained>true</SelfContained>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<!-- Ignore vulnerable (framework) packages; this app is not published. -->
<NoWarn>NU1902;NU1903</NoWarn>
</PropertyGroup>

<ItemGroup Condition="$(Configuration) == 'Debug'">
<ProjectReference Include="..\..\src\Serilog.Settings.Configuration\Serilog.Settings.Configuration.csproj" />
</ItemGroup>

<ItemGroup Condition="$(Configuration) == 'Release'">
<PackageReference Include="Serilog.Settings.Configuration" Version="[0.0.0-IntegrationTest.0]" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Costura.Fody" Version="5.7.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions test/TestApp-net9.0/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value="packages" />
</config>
<packageSources>
<clear />
<add key="local" value="." />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
5 changes: 2 additions & 3 deletions test/TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
<SelfContained>true</SelfContained>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<!-- Ignore vulnerable (framework) packages; this app is not published. -->
<NoWarn>NU1902;NU1903</NoWarn>
</PropertyGroup>

<ItemGroup Condition="$(Configuration) == 'Debug'">
Expand All @@ -26,9 +28,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Private.Uri" Version="4.3.2" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>

</Project>

0 comments on commit 120d753

Please sign in to comment.