Clean up logging work (#1341)
* clean up logging work * update the main readme file * fix broken link
This commit is contained in:
parent
e381b395cb
commit
f68157f49e
|
|
@ -200,6 +200,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started", "docs\log
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Exporter.InMemory", "src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj", "{9BCEA68B-50E2-4A3A-93E6-B51AF612BCC1}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "extending-the-sdk", "docs\logs\extending-the-sdk\extending-the-sdk.csproj", "{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -390,6 +392,10 @@ Global
|
|||
{9BCEA68B-50E2-4A3A-93E6-B51AF612BCC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9BCEA68B-50E2-4A3A-93E6-B51AF612BCC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9BCEA68B-50E2-4A3A-93E6-B51AF612BCC1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -419,6 +425,7 @@ Global
|
|||
{B26BE278-C9DA-4067-A0EE-6A4227B3DC87} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
|
||||
{DB942F5A-D571-4DEA-B1A7-B6BE0E24E6ED} = {E359BB2B-9AEC-497D-B321-7DF2450C3B8E}
|
||||
{B3F03725-23A0-4582-9526-F6A7E38F35CC} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
|
||||
{13C10C9A-07E8-43EB-91F5-C2B116FBE0FC} = {3862190B-E2C5-418E-AFDC-DB281FB5C705}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {55639B5C-0770-4A22-AB56-859604650521}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,8 @@ most common customization and extension scenarios:
|
|||
|
||||
* [Building a custom instrumentation
|
||||
library](./docs/trace/extending-the-sdk/README.md#instrumentation-library)
|
||||
* [Building a custom log
|
||||
exporter/processor/sampler](./docs/logs/extending-the-sdk/README.md)
|
||||
* [Building a custom trace
|
||||
exporter/processor/sampler](./docs/trace/extending-the-sdk/README.md)
|
||||
* [Correlating logs with traces](./docs/logs/correlation/README.md)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Label="Package versions used in this folder">
|
||||
|
|
|
|||
|
|
@ -21,17 +21,6 @@ using OpenTelemetry.Logs;
|
|||
|
||||
internal static class LoggerExtensions
|
||||
{
|
||||
// https://docs.microsoft.com/aspnet/core/fundamentals/logging/loggermessage
|
||||
private static readonly Action<ILogger, object, Exception> LogExAction = LoggerMessage.Define<object>(
|
||||
LogLevel.Information,
|
||||
new EventId(1, nameof(LogEx)),
|
||||
"LogEx({obj}).");
|
||||
|
||||
public static void LogEx(this ILogger logger, object obj)
|
||||
{
|
||||
LogExAction(logger, obj, null);
|
||||
}
|
||||
|
||||
public static OpenTelemetryLoggerOptions AddMyExporter(this OpenTelemetryLoggerOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
// <copyright file="Program.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System.Collections.Generic;
|
||||
#if NETCOREAPP2_1
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
#endif
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Logs;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
#if NETCOREAPP2_1
|
||||
var serviceCollection = new ServiceCollection().AddLogging(builder =>
|
||||
#else
|
||||
using var loggerFactory = LoggerFactory.Create(builder =>
|
||||
#endif
|
||||
{
|
||||
builder.AddOpenTelemetry(options => options
|
||||
.AddProcessor(new MyProcessor("ProcessorA"))
|
||||
.AddProcessor(new MyProcessor("ProcessorB"))
|
||||
.AddProcessor(new SimpleExportProcessor<LogRecord>(new MyExporter("ExporterX")))
|
||||
.AddMyExporter());
|
||||
});
|
||||
|
||||
#if NETCOREAPP2_1
|
||||
using var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
|
||||
#else
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
#endif
|
||||
|
||||
// unstructured log
|
||||
logger.LogInformation("Hello, World!");
|
||||
|
||||
// unstructured log with string interpolation
|
||||
logger.LogInformation($"Hello from potato {0.99}.");
|
||||
|
||||
// structured log with template
|
||||
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
|
||||
|
||||
// structured log with strong type
|
||||
logger.LogInformation("{food}", new Food { Name = "artichoke", Price = 3.99 });
|
||||
|
||||
// structured log with anonymous type
|
||||
logger.LogInformation("{food}", new { Name = "pumpkin", Price = 5.99 });
|
||||
|
||||
// structured log with general type
|
||||
logger.LogInformation("{food}", new Dictionary<string, object>
|
||||
{
|
||||
["Name"] = "truffle",
|
||||
["Price"] = 299.99,
|
||||
});
|
||||
}
|
||||
|
||||
internal struct Food
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public double Price { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# Extending the OpenTelemetry .NET SDK
|
||||
|
||||
* [Building your own exporter](#exporter)
|
||||
* [Building your own processor](#processor)
|
||||
* [Building your own sampler](#sampler)
|
||||
* [References](#references)
|
||||
|
||||
## Exporter
|
||||
|
||||
TBD
|
||||
|
||||
## Processor
|
||||
|
||||
TBD
|
||||
|
||||
## Sampler
|
||||
|
||||
TBD
|
||||
|
||||
## References
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp2.1'">
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System.Collections.Generic;
|
||||
#if NETCOREAPP2_1
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
#endif
|
||||
|
|
@ -31,9 +30,7 @@ public class Program
|
|||
#endif
|
||||
{
|
||||
builder.AddOpenTelemetry(options => options
|
||||
.AddProcessor(new MyProcessor("A"))
|
||||
.AddProcessor(new MyProcessor("B"))
|
||||
.AddMyExporter());
|
||||
.AddInMemoryExporter()); // TODO: change to console output
|
||||
});
|
||||
|
||||
#if NETCOREAPP2_1
|
||||
|
|
@ -43,33 +40,6 @@ public class Program
|
|||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
#endif
|
||||
|
||||
// unstructured log
|
||||
logger.LogInformation("Hello, World!");
|
||||
|
||||
// unstructured log with string interpolation
|
||||
logger.LogInformation($"Hello from potato {0.99}.");
|
||||
|
||||
// structured log with template
|
||||
logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
|
||||
|
||||
// structured log with strong type
|
||||
logger.LogEx(new Food { Name = "artichoke", Price = 3.99 });
|
||||
|
||||
// structured log with anonymous type
|
||||
logger.LogEx(new { Name = "pumpkin", Price = 5.99 });
|
||||
|
||||
// structured log with general type
|
||||
logger.LogEx(new Dictionary<string, object>
|
||||
{
|
||||
["Name"] = "truffle",
|
||||
["Price"] = 299.99,
|
||||
});
|
||||
}
|
||||
|
||||
internal struct Food
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public double Price { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
|
||||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
|
||||
|
|
|
|||
|
|
@ -17,9 +17,6 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using OpenTelemetry.Exporter;
|
||||
#if NETSTANDARD2_0
|
||||
using OpenTelemetry.Logs;
|
||||
#endif
|
||||
using OpenTelemetry.Trace;
|
||||
|
||||
namespace OpenTelemetry
|
||||
|
|
@ -44,20 +41,5 @@ namespace OpenTelemetry
|
|||
configure?.Invoke(options);
|
||||
return builder.AddProcessor(new SimpleExportProcessor<Activity>(new InMemoryExporter<Activity>(options)));
|
||||
}
|
||||
|
||||
#if NETSTANDARD2_0
|
||||
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<InMemoryExporterOptions> configure = null)
|
||||
{
|
||||
if (loggerOptions == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerOptions));
|
||||
}
|
||||
|
||||
var options = new InMemoryExporterOptions();
|
||||
configure?.Invoke(options);
|
||||
|
||||
return loggerOptions.AddProcessor(new SimpleExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(options)));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
// <copyright file="InMemoryExporterLoggingExtensions.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
#if NETSTANDARD2_0
|
||||
using System;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Exporter;
|
||||
using OpenTelemetry.Logs;
|
||||
|
||||
namespace Microsoft.Extensions.Logging
|
||||
{
|
||||
public static class InMemoryExporterLoggingExtensions
|
||||
{
|
||||
public static OpenTelemetryLoggerOptions AddInMemoryExporter(this OpenTelemetryLoggerOptions loggerOptions, Action<InMemoryExporterOptions> configure = null)
|
||||
{
|
||||
if (loggerOptions == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerOptions));
|
||||
}
|
||||
|
||||
var options = new InMemoryExporterOptions();
|
||||
configure?.Invoke(options);
|
||||
|
||||
return loggerOptions.AddProcessor(new SimpleExportProcessor<LogRecord>(new InMemoryExporter<LogRecord>(options)));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Loading…
Reference in New Issue