-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ILogger integration - part 1 (#1308)
* ilogger integration * update changelog * s/name/categoryName/g * TODO comment
- Loading branch information
Showing
12 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// <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 Microsoft.Extensions.Logging; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddOpenTelemetry(); | ||
}); | ||
var logger = loggerFactory.CreateLogger<Program>(); | ||
|
||
logger.LogInformation("Hello, World!"); | ||
logger.LogInformation("Hello from {name} {price}.", "artichoke", 3.99); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Getting Started with OpenTelemetry .NET in 5 Minutes | ||
|
||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" /> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// <copyright file="OpenTelemetryLogger.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 System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
internal class OpenTelemetryLogger : ILogger | ||
{ | ||
private readonly string categoryName; | ||
|
||
internal OpenTelemetryLogger(string categoryName, OpenTelemetryLoggerOptions options) | ||
{ | ||
this.categoryName = categoryName ?? throw new ArgumentNullException(nameof(categoryName)); | ||
|
||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
} | ||
|
||
internal IExternalScopeProvider ScopeProvider { get; set; } | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | ||
{ | ||
if (!this.IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
var timestamp = DateTime.UtcNow; | ||
|
||
if (state is IReadOnlyCollection<KeyValuePair<string, object>> dict) | ||
{ | ||
var isUnstructuredLog = dict.Count == 1; | ||
|
||
// TODO: remove the console output after finished the plumbing work to log processors/exporters | ||
if (isUnstructuredLog) | ||
{ | ||
foreach (var entry in dict) | ||
{ | ||
Console.WriteLine($"{this.categoryName}({logLevel}, Id={eventId}): {entry.Value}"); | ||
} | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"{this.categoryName}({logLevel}, Id={eventId}):"); | ||
foreach (var entry in dict) | ||
{ | ||
if (string.Equals(entry.Key, "{OriginalFormat}", StringComparison.Ordinal)) | ||
{ | ||
Console.WriteLine($" $format: {entry.Value}"); | ||
continue; | ||
} | ||
|
||
Console.WriteLine($" {entry.Key}: {entry.Value}"); | ||
} | ||
} | ||
|
||
if (exception != null) | ||
{ | ||
Console.WriteLine($" $exception: {exception}"); | ||
} | ||
} | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return logLevel != LogLevel.None; | ||
} | ||
|
||
public IDisposable BeginScope<TState>(TState state) => this.ScopeProvider?.Push(state) ?? null; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// <copyright file="OpenTelemetryLoggerOptions.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 Microsoft.Extensions.Logging; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
public class OpenTelemetryLoggerOptions | ||
{ | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// <copyright file="OpenTelemetryLoggerProvider.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 System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
[ProviderAlias("OpenTelemetry")] | ||
public class OpenTelemetryLoggerProvider : ILoggerProvider, ISupportExternalScope | ||
{ | ||
private readonly IOptionsMonitor<OpenTelemetryLoggerOptions> options; | ||
private readonly IDictionary<string, ILogger> loggers; | ||
private IExternalScopeProvider scopeProvider; | ||
|
||
public OpenTelemetryLoggerProvider(IOptionsMonitor<OpenTelemetryLoggerOptions> options) | ||
{ | ||
this.options = options; | ||
this.loggers = new Dictionary<string, ILogger>(StringComparer.Ordinal); | ||
} | ||
|
||
internal IExternalScopeProvider ScopeProvider | ||
{ | ||
get | ||
{ | ||
if (this.scopeProvider == null) | ||
{ | ||
this.scopeProvider = new LoggerExternalScopeProvider(); | ||
} | ||
|
||
return this.scopeProvider; | ||
} | ||
} | ||
|
||
void ISupportExternalScope.SetScopeProvider(IExternalScopeProvider scopeProvider) | ||
{ | ||
// TODO: set existing loggers | ||
this.scopeProvider = scopeProvider; | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
lock (this.loggers) | ||
{ | ||
ILogger logger; | ||
|
||
if (this.loggers.TryGetValue(categoryName, out logger)) | ||
{ | ||
return logger; | ||
} | ||
|
||
logger = new OpenTelemetryLogger(categoryName, this.options.CurrentValue); | ||
this.loggers.Add(categoryName, logger); | ||
return logger; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// <copyright file="OpenTelemetryLoggingExtensions.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 Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Configuration; | ||
using OpenTelemetry.Logs; | ||
|
||
namespace Microsoft.Extensions.Logging | ||
{ | ||
public static class OpenTelemetryLoggingExtensions | ||
{ | ||
public static ILoggingBuilder AddOpenTelemetry(this ILoggingBuilder builder, Action<OpenTelemetryLoggerOptions> configure = null) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AddConfiguration(); | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, OpenTelemetryLoggerProvider>()); | ||
|
||
if (configure != null) | ||
{ | ||
builder.Services.Configure(configure); | ||
} | ||
|
||
return builder; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters