Skip to content

Commit ca220cb

Browse files
Wuyafengniemyjski
authored andcommitted
Resolve issue #211
- Add ILoggingBuilder extension methods - Markup ILoggerFactory extension methods obsolute - Add sample codes in SampleAspNetCore
1 parent 97e234e commit ca220cb

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

samples/Exceptionless.SampleAspNetCore/Startup.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public void ConfigureServices(IServiceCollection services) {
2222
services.AddLogging(b => b
2323
.AddConfiguration(Configuration.GetSection("Logging"))
2424
.AddDebug()
25-
.AddConsole());
25+
.AddConsole()
26+
.AddExceptionless());
2627
services.AddHttpContextAccessor();
2728
services.AddMvc();
2829
}
@@ -38,7 +39,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
3839
//OR
3940
//loggerFactory.AddExceptionless((c) => c.ReadFromConfiguration(Configuration));
4041

41-
loggerFactory.AddExceptionless();
42+
//loggerFactory.AddExceptionless();
4243
app.UseRouting();
4344
app.UseEndpoints(endpoints => {
4445
endpoints.MapControllers();

src/Platforms/Exceptionless.Extensions.Logging/Exceptionless.Extensions.Logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup Label="Package References">
18-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
18+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

src/Platforms/Exceptionless.Extensions.Logging/ExceptionlessLoggerExtensions.cs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,56 @@ public static ExceptionlessLogLevel ToLogLevel(this LogLevel level) {
2323

2424
return ExceptionlessLogLevel.Off;
2525
}
26+
/// <summary>
27+
/// Adds Exceptionless to the logging pipeline using the <see cref="ExceptionlessClient.Default"/>.
28+
/// </summary>
29+
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
30+
/// <param name="client">If a client is not specified then the <see cref="ExceptionlessClient.Default"/> wil be used.</param>
31+
/// <returns>The <see cref="ILoggingBuilder"/>.</returns>
32+
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, ExceptionlessClient client = null) {
33+
builder.AddProvider(new ExceptionlessLoggerProvider(client ?? ExceptionlessClient.Default));
34+
return builder;
35+
}
36+
/// <summary>
37+
/// Adds Exceptionless to the logging pipeline using a new client with the provided api key.
38+
/// </summary>
39+
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
40+
/// <param name="apiKey">The project api key.</param>
41+
/// <param name="serverUrl">The Server Url.</param>
42+
/// <returns>The <see cref="ILoggingBuilder"/>.</returns>
43+
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, string apiKey, string serverUrl = null) {
44+
if (String.IsNullOrEmpty(apiKey) && String.IsNullOrEmpty(serverUrl))
45+
return builder.AddExceptionless();
2646

27-
// TODO: Add support for ILoggingBuilder
47+
builder.AddProvider(new ExceptionlessLoggerProvider(config => {
48+
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
49+
config.ApiKey = apiKey;
50+
if (!String.IsNullOrEmpty(serverUrl))
51+
config.ServerUrl = serverUrl;
2852

53+
config.UseInMemoryStorage();
54+
}));
55+
56+
return builder;
57+
}
2958
/// <summary>
30-
/// Adds Exceptionless to the logging pipeline using the default client.
59+
/// Adds Exceptionless to the logging pipeline using a new client configured with the provided action.
60+
/// </summary>
61+
/// <param name="builder">The <see cref="ILoggingBuilder"/>.</param>
62+
/// <param name="configure">An <see cref="Action{ExceptionlessConfiguration}"/> that applies additional settings and plugins. The project api key must be specified.</param>
63+
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
64+
public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, Action<ExceptionlessConfiguration> configure) {
65+
builder.AddProvider(new ExceptionlessLoggerProvider(configure));
66+
return builder;
67+
}
68+
#region Obsolute
69+
/// <summary>
70+
/// Adds Exceptionless to the logging pipeline using the <see cref="ExceptionlessClient.Default"/>.
3171
/// </summary>
3272
/// <param name="factory">The <see cref="ILoggerFactory"/>.</param>
33-
/// <param name="client">If a client is not specified than the default instance will be used.</param>
73+
/// <param name="client">If a client is not specified then the <see cref="ExceptionlessClient.Default"/> wil be used.</param>
3474
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
75+
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,ExceptionlessClient) instead.")]
3576
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, ExceptionlessClient client = null) {
3677
factory.AddProvider(new ExceptionlessLoggerProvider(client ?? ExceptionlessClient.Default));
3778
return factory;
@@ -44,6 +85,7 @@ public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, Excep
4485
/// <param name="apiKey">The project api key.</param>
4586
/// <param name="serverUrl">The Server Url</param>
4687
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
88+
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,string,string) instead.")]
4789
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, string apiKey, string serverUrl = null) {
4890
if (String.IsNullOrEmpty(apiKey) && String.IsNullOrEmpty(serverUrl))
4991
return factory.AddExceptionless();
@@ -66,9 +108,11 @@ public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, strin
66108
/// <param name="factory">The <see cref="ILoggerFactory"/>.</param>
67109
/// <param name="configure">An <see cref="Action{ExceptionlessConfiguration}"/> that applies additional settings and plugins. The project api key must be specified.</param>
68110
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
111+
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,Action<ExceptionlessConfiguration>) instead.")]
69112
public static ILoggerFactory AddExceptionless(this ILoggerFactory factory, Action<ExceptionlessConfiguration> configure) {
70113
factory.AddProvider(new ExceptionlessLoggerProvider(configure));
71114
return factory;
72-
}
115+
}
116+
#endregion
73117
}
74118
}

0 commit comments

Comments
 (0)