Skip to content

Commit de0360e

Browse files
authored
Enable writing json format string as log message (#184)
1 parent aabfc12 commit de0360e

File tree

12 files changed

+787
-653
lines changed

12 files changed

+787
-653
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.Extensions.Logging;
7+
using nanoFramework.Logging;
8+
using nanoFramework.Logging.Debug;
9+
using nanoFramework.TestFramework;
10+
using System;
11+
using System.Diagnostics;
12+
13+
namespace UnitTestDebugLogging
14+
{
15+
[TestClass]
16+
public class LoggerExtensionsTests
17+
{
18+
private static ILogger _logger;
19+
20+
/// <summary>
21+
/// Initializes a static logger in the debug output. If none of the function throughs, then the output should be correct.
22+
/// </summary>
23+
[Setup]
24+
public void GetLogger()
25+
{
26+
_logger = new DebugLogger("LoggerExtensionsTests");
27+
((DebugLogger)_logger).MinLogLevel = LogLevel.Trace;
28+
}
29+
30+
/// <summary>
31+
/// LogInformation Test with formatted string with some parameters.
32+
/// </summary>
33+
[TestMethod]
34+
public void LogInformationFormatedStringWithParams()
35+
{
36+
_logger.LogInformation("{0}{1}{2}", "nano", "frame", "work");
37+
}
38+
39+
/// <summary>
40+
/// LogWarning Test with formatted string with some parameters.
41+
/// </summary>
42+
[TestMethod]
43+
public void LogWarningFormatedStringWithParams()
44+
{
45+
_logger.LogWarning("{0}{1}{2}", "nano", "frame", "work");
46+
}
47+
48+
/// <summary>
49+
/// LogTrace Test with formatted string with some parameters.
50+
/// </summary>
51+
[TestMethod]
52+
public void LogTraceFormatedStringWithParams()
53+
{
54+
_logger.LogTrace("{0}{1}{2}", "nano", "frame", "work");
55+
}
56+
57+
/// <summary>
58+
/// LogError Test with formatted string with some parameters.
59+
/// </summary>
60+
[TestMethod]
61+
public void LogErrorFormatedStringWithParams()
62+
{
63+
_logger.LogError("{0}{1}{2}", "nano", "frame", "work");
64+
}
65+
66+
/// <summary>
67+
/// LogDebug Test with formatted string with some parameters.
68+
/// </summary>
69+
[TestMethod]
70+
public void LogDebugFormatedStringWithParams()
71+
{
72+
_logger.LogDebug("{0}{1}{2}", "nano", "frame", "work");
73+
}
74+
75+
/// <summary>
76+
/// LogCritical Test with formatted string with some parameters.
77+
/// </summary>
78+
[TestMethod]
79+
public void LogCriticalFormatedStringWithParams()
80+
{
81+
_logger.LogCritical("{0}{1}{2}", "nano", "frame", "work");
82+
}
83+
84+
/// <summary>
85+
/// Log Test with formatted string with some parameters and all log levels.
86+
/// </summary>
87+
[TestMethod]
88+
public void LogFormatedStringWithParams()
89+
{
90+
for (int i = 0; i < 7; i++)
91+
{
92+
_logger.Log((LogLevel)i, "{0}{1}{2}", "nano", "frame", "work");
93+
}
94+
}
95+
96+
/// <summary>
97+
/// string.Format on a json string doesn't work well.
98+
/// </summary>
99+
[TestMethod]
100+
public void LogJsonStringWithParams()
101+
{
102+
Assert.ThrowsException(typeof(ArgumentException), LogJsonStringWithParamsThrowEx);
103+
}
104+
105+
/// <summary>
106+
/// Incorrect call.
107+
/// </summary>
108+
private void LogJsonStringWithParamsThrowEx()
109+
{
110+
_logger.LogError(@"{ ""Message"":""nanoframework"" }", 1, 2);
111+
}
112+
113+
/// <summary>
114+
/// Logging a json formatted string.
115+
/// </summary>
116+
[TestMethod]
117+
public void LogJsonStringWithoutParams()
118+
{
119+
_logger.LogInformation(@"{ ""Message"":""nanoframework"" }");
120+
}
121+
122+
/// <summary>
123+
/// Logging with null params.
124+
/// </summary>
125+
[TestMethod]
126+
public void LogNullArgumentExtension()
127+
{
128+
_logger.Log(LogLevel.Debug, "Null arguments", null);
129+
}
130+
131+
/// <summary>
132+
/// Logging with empty params.
133+
/// </summary>
134+
[TestMethod]
135+
public void LogEmptyArgumentExtension()
136+
{
137+
_logger.Log(LogLevel.Debug, "Null arguments", new object[0]);
138+
}
139+
}
140+
}
Lines changed: 99 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,100 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Label="Globals">
4-
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5-
</PropertyGroup>
6-
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7-
<ItemGroup>
8-
<ProjectCapability Include="TestContainer" />
9-
</ItemGroup>
10-
<PropertyGroup>
11-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
13-
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14-
<ProjectGuid>e7ee659c-d98c-43ea-a71a-45758cbf22a4</ProjectGuid>
15-
<OutputType>Library</OutputType>
16-
<AppDesignerFolder>Properties</AppDesignerFolder>
17-
<FileAlignment>512</FileAlignment>
18-
<RootNamespace>UnitTestDebugLogging</RootNamespace>
19-
<AssemblyName>NFUnitTest</AssemblyName>
20-
<IsCodedUITest>False</IsCodedUITest>
21-
<IsTestProject>true</IsTestProject>
22-
<TestProjectType>UnitTest</TestProjectType>
23-
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24-
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
25-
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
26-
</PropertyGroup>
27-
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
28-
<PropertyGroup>
29-
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
30-
</PropertyGroup>
31-
<ItemGroup>
32-
<Compile Include="DebugTest.cs" />
33-
<Compile Include="EventIdTests.cs" />
34-
<Compile Include="FormattingTest.cs" />
35-
<Compile Include="MemoryStreamTests.cs" />
36-
<Compile Include="MyTestComponent.cs" />
37-
<Compile Include="Properties\AssemblyInfo.cs" />
38-
<Compile Include="SerialTest.cs" />
39-
<Compile Include="StreamTests.cs" />
40-
<Compile Include="TestLogLevels.cs" />
41-
<Compile Include="TestWithNullLogger.cs" />
42-
</ItemGroup>
43-
<ItemGroup>
44-
<Reference Include="mscorlib, Version=1.14.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
45-
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
46-
<Private>True</Private>
47-
</Reference>
48-
<Reference Include="nanoFramework.Hardware.Esp32, Version=1.5.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
49-
<HintPath>..\..\packages\nanoFramework.Hardware.Esp32.1.5.1\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
50-
<Private>True</Private>
51-
</Reference>
52-
<Reference Include="nanoFramework.Runtime.Events, Version=1.11.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
53-
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
54-
<Private>True</Private>
55-
</Reference>
56-
<Reference Include="nanoFramework.System.Collections, Version=1.5.18.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
57-
<HintPath>..\..\packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll</HintPath>
58-
<Private>True</Private>
59-
</Reference>
60-
<Reference Include="nanoFramework.System.Text, Version=1.2.37.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
61-
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll</HintPath>
62-
<Private>True</Private>
63-
</Reference>
64-
<Reference Include="nanoFramework.TestFramework, Version=2.1.55.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
65-
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.55\lib\nanoFramework.TestFramework.dll</HintPath>
66-
<Private>True</Private>
67-
</Reference>
68-
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
69-
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.55\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
70-
<Private>True</Private>
71-
</Reference>
72-
<Reference Include="System.IO.FileSystem, Version=1.1.23.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
73-
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.23\lib\System.IO.FileSystem.dll</HintPath>
74-
<Private>True</Private>
75-
</Reference>
76-
<Reference Include="System.IO.Ports, Version=1.1.60.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
77-
<HintPath>..\..\packages\nanoFramework.System.IO.Ports.1.1.60\lib\System.IO.Ports.dll</HintPath>
78-
<Private>True</Private>
79-
</Reference>
80-
<Reference Include="System.IO.Streams, Version=1.1.38.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
81-
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.38\lib\System.IO.Streams.dll</HintPath>
82-
<Private>True</Private>
83-
</Reference>
84-
</ItemGroup>
85-
<ItemGroup>
86-
<None Include="packages.config" />
87-
</ItemGroup>
88-
<ItemGroup>
89-
<ProjectReference Include="..\..\nanoFramework.Logging.Serial\nanoFramework.Logging.Serial.nfproj" />
90-
<ProjectReference Include="..\..\nanoFramework.Logging.Stream\nanoFramework.Logging.Stream.nfproj" />
91-
<ProjectReference Include="..\..\nanoFramework.Logging\nanoFramework.Logging.nfproj" />
92-
</ItemGroup>
93-
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
94-
<!-- MANUAL UPDATE HERE -->
95-
<ProjectExtensions>
96-
<ProjectCapabilities>
97-
<ProjectConfigurationsDeclaredAsItems />
98-
</ProjectCapabilities>
99-
</ProjectExtensions>
100-
<Import Project="..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets')" />
101-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
102-
<PropertyGroup>
103-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
104-
</PropertyGroup>
105-
<Error Condition="!Exists('..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets'))" />
106-
</Target>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<ItemGroup>
8+
<ProjectCapability Include="TestContainer" />
9+
</ItemGroup>
10+
<PropertyGroup>
11+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
13+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<ProjectGuid>e7ee659c-d98c-43ea-a71a-45758cbf22a4</ProjectGuid>
15+
<OutputType>Library</OutputType>
16+
<AppDesignerFolder>Properties</AppDesignerFolder>
17+
<FileAlignment>512</FileAlignment>
18+
<RootNamespace>UnitTestDebugLogging</RootNamespace>
19+
<AssemblyName>NFUnitTest</AssemblyName>
20+
<IsCodedUITest>False</IsCodedUITest>
21+
<IsTestProject>true</IsTestProject>
22+
<TestProjectType>UnitTest</TestProjectType>
23+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
25+
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
26+
</PropertyGroup>
27+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
28+
<ItemGroup>
29+
<Compile Include="DebugTest.cs" />
30+
<Compile Include="EventIdTests.cs" />
31+
<Compile Include="FormattingTest.cs" />
32+
<Compile Include="LoggerExtensionsTests.cs" />
33+
<Compile Include="MemoryStreamTests.cs" />
34+
<Compile Include="MyTestComponent.cs" />
35+
<Compile Include="Properties\AssemblyInfo.cs" />
36+
<Compile Include="SerialTest.cs" />
37+
<Compile Include="StreamTests.cs" />
38+
<Compile Include="TestLogLevels.cs" />
39+
<Compile Include="TestWithNullLogger.cs" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Reference Include="mscorlib">
43+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
44+
</Reference>
45+
<Reference Include="nanoFramework.Hardware.Esp32">
46+
<HintPath>..\..\packages\nanoFramework.Hardware.Esp32.1.5.1\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
47+
</Reference>
48+
<Reference Include="nanoFramework.Runtime.Events">
49+
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
50+
</Reference>
51+
<Reference Include="nanoFramework.System.Collections">
52+
<HintPath>..\..\packages\nanoFramework.System.Collections.1.5.18\lib\nanoFramework.System.Collections.dll</HintPath>
53+
</Reference>
54+
<Reference Include="nanoFramework.System.Text">
55+
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.37\lib\nanoFramework.System.Text.dll</HintPath>
56+
</Reference>
57+
<Reference Include="nanoFramework.TestFramework, Version=2.1.55.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
58+
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.55\lib\nanoFramework.TestFramework.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
61+
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
62+
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.55\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
63+
<Private>True</Private>
64+
</Reference>
65+
<Reference Include="System.IO.FileSystem">
66+
<HintPath>..\..\packages\nanoFramework.System.IO.FileSystem.1.1.23\lib\System.IO.FileSystem.dll</HintPath>
67+
</Reference>
68+
<Reference Include="System.IO.Ports">
69+
<HintPath>..\..\packages\nanoFramework.System.IO.Ports.1.1.60\lib\System.IO.Ports.dll</HintPath>
70+
</Reference>
71+
<Reference Include="System.IO.Streams">
72+
<HintPath>..\..\packages\nanoFramework.System.IO.Streams.1.1.38\lib\System.IO.Streams.dll</HintPath>
73+
</Reference>
74+
</ItemGroup>
75+
<ItemGroup>
76+
<None Include="packages.config" />
77+
</ItemGroup>
78+
<ItemGroup>
79+
<ProjectReference Include="..\..\nanoFramework.Logging.Serial\nanoFramework.Logging.Serial.nfproj" />
80+
<ProjectReference Include="..\..\nanoFramework.Logging.Stream\nanoFramework.Logging.Stream.nfproj" />
81+
<ProjectReference Include="..\..\nanoFramework.Logging\nanoFramework.Logging.nfproj" />
82+
</ItemGroup>
83+
<ItemGroup>
84+
<Content Include="packages.lock.json" />
85+
</ItemGroup>
86+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
87+
<!-- MANUAL UPDATE HERE -->
88+
<ProjectExtensions>
89+
<ProjectCapabilities>
90+
<ProjectConfigurationsDeclaredAsItems />
91+
</ProjectCapabilities>
92+
</ProjectExtensions>
93+
<Import Project="..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets')" />
94+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
95+
<PropertyGroup>
96+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
97+
</PropertyGroup>
98+
<Error Condition="!Exists('..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\nanoFramework.TestFramework.2.1.55\build\nanoFramework.TestFramework.targets'))" />
99+
</Target>
107100
</Project>

0 commit comments

Comments
 (0)