Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ILoggingBuilder extension methods #226

Merged
merged 2 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions samples/Exceptionless.SampleAspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void ConfigureServices(IServiceCollection services) {
services.AddLogging(b => b
.AddConfiguration(Configuration.GetSection("Logging"))
.AddDebug()
.AddConsole());
.AddConsole()
.AddExceptionless());
services.AddHttpContextAccessor();
services.AddMvc();
}
Expand All @@ -38,7 +39,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
//OR
//loggerFactory.AddExceptionless((c) => c.ReadFromConfiguration(Configuration));

loggerFactory.AddExceptionless();
//loggerFactory.AddExceptionless();
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup Label="Package References">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we can't keep using the abstractions lib and target 2.2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to use ILoggingBuilder,I have to use Microsoft.Extensions.Logging instead of Microsoft.Extensions.Logging.Abstractions.
It seems reasonable to use 2.2 not 3.1.

</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,56 @@ public static ExceptionlessLogLevel ToLogLevel(this LogLevel level) {

return ExceptionlessLogLevel.Off;
}
/// <summary>
/// Adds Exceptionless to the logging pipeline using the <see cref="ExceptionlessClient.Default"/>.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
/// <param name="client">If a client is not specified then the <see cref="ExceptionlessClient.Default"/> wil be used.</param>
/// <returns>The <see cref="ILoggingBuilder"/>.</returns>
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, ExceptionlessClient client = null) {
builder.AddProvider(new ExceptionlessLoggerProvider(client ?? ExceptionlessClient.Default));
return builder;
}
/// <summary>
/// Adds Exceptionless to the logging pipeline using a new client with the provided api key.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
/// <param name="apiKey">The project api key.</param>
/// <param name="serverUrl">The Server Url.</param>
/// <returns>The <see cref="ILoggingBuilder"/>.</returns>
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, string apiKey, string serverUrl = null) {
if (String.IsNullOrEmpty(apiKey) && String.IsNullOrEmpty(serverUrl))
return builder.AddExceptionless();

// TODO: Add support for ILoggingBuilder
builder.AddProvider(new ExceptionlessLoggerProvider(config => {
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
config.ApiKey = apiKey;
if (!String.IsNullOrEmpty(serverUrl))
config.ServerUrl = serverUrl;

config.UseInMemoryStorage();
}));

return builder;
}
/// <summary>
/// Adds Exceptionless to the logging pipeline using the default client.
/// Adds Exceptionless to the logging pipeline using a new client configured with the provided action.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
/// <param name="configure">An <see cref="Action{ExceptionlessConfiguration}"/> that applies additional settings and plugins. The project api key must be specified.</param>
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, Action<ExceptionlessConfiguration> configure) {
builder.AddProvider(new ExceptionlessLoggerProvider(configure));
return builder;
}
#region Obsolute
wu-yafeng marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Adds Exceptionless to the logging pipeline using the <see cref="ExceptionlessClient.Default"/>.
/// </summary>
/// <param name="factory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="client">If a client is not specified than the default instance will be used.</param>
/// <param name="client">If a client is not specified then the <see cref="ExceptionlessClient.Default"/> wil be used.</param>
wu-yafeng marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,ExceptionlessClient) instead.")]
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, ExceptionlessClient client = null) {
factory.AddProvider(new ExceptionlessLoggerProvider(client ?? ExceptionlessClient.Default));
return factory;
Expand All @@ -44,6 +85,7 @@ public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, Excep
/// <param name="apiKey">The project api key.</param>
/// <param name="serverUrl">The Server Url</param>
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,string,string) instead.")]
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, string apiKey, string serverUrl = null) {
if (String.IsNullOrEmpty(apiKey) && String.IsNullOrEmpty(serverUrl))
return factory.AddExceptionless();
Expand All @@ -66,9 +108,11 @@ public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, strin
/// <param name="factory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="configure">An <see cref="Action{ExceptionlessConfiguration}"/> that applies additional settings and plugins. The project api key must be specified.</param>
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,Action<ExceptionlessConfiguration>) instead.")]
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, Action<ExceptionlessConfiguration> configure) {
factory.AddProvider(new ExceptionlessLoggerProvider(configure));
return factory;
}
}
#endregion
}
}