Skip to content

Commit

Permalink
Clean up logging work (open-telemetry#1341)
Browse files Browse the repository at this point in the history
* clean up logging work

* update the main readme file

* fix broken link
  • Loading branch information
reyang authored Oct 12, 2020
1 parent e381b39 commit f68157f
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 66 deletions.
7 changes: 7 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@

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)
Expand Down
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions docs/logs/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
20 changes: 20 additions & 0 deletions docs/logs/extending-the-sdk/README.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions docs/logs/extending-the-sdk/extending-the-sdk.csproj
Original file line number Diff line number Diff line change
@@ -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>
32 changes: 1 addition & 31 deletions docs/logs/getting-started/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System.Collections.Generic;
#if NETCOREAPP2_1
using Microsoft.Extensions.DependencyInjection;
#endif
Expand All @@ -31,9 +30,7 @@ public static void Main()
#endif
{
builder.AddOpenTelemetry(options => options
.AddProcessor(new MyProcessor("A"))
.AddProcessor(new MyProcessor("B"))
.AddMyExporter());
.AddInMemoryExporter()); // TODO: change to console output
});

#if NETCOREAPP2_1
Expand All @@ -43,33 +40,6 @@ public static void Main()
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; }
}
}
6 changes: 1 addition & 5 deletions docs/logs/getting-started/getting-started.csproj
Original file line number Diff line number Diff line change
@@ -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'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
using System;
using System.Diagnostics;
using OpenTelemetry.Exporter;
#if NETSTANDARD2_0
using OpenTelemetry.Logs;
#endif
using OpenTelemetry.Trace;

namespace OpenTelemetry
Expand All @@ -44,20 +41,5 @@ public static TracerProviderBuilder AddInMemoryExporter(this TracerProviderBuild
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
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f68157f

Please sign in to comment.