Skip to content

Commit

Permalink
#Fix 260, Add minimum log fixes for Microsoft.Extensions.Logging, Log…
Browse files Browse the repository at this point in the history
…4net

I also fixed the case for set default min log level on NLog when using the default client.
  • Loading branch information
niemyjski committed May 1, 2023
1 parent 941e983 commit 580eea7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static ILoggingBuilder AddExceptionless(this ILoggingBuilder builder, Act
/// <param name="factory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="client">If a client is not specified then the <see cref="ExceptionlessClient.Default"/> will be used.</param>
/// <returns>The <see cref="ILoggerFactory"/>.</returns>
[Obsolete("Use ExceptionlessLoggerExtensions.AddExceptionless(ILoggingBuilder,ExceptionlessClient) instead.")]
[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 @@ -102,7 +102,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.")]
[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 @@ -125,7 +125,7 @@ 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.")]
[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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ public class ExceptionlessLoggerProvider : ILoggerProvider {
/// </summary>
public ExceptionlessLoggerProvider(ExceptionlessClient client) {
_client = client ?? throw new ArgumentNullException(nameof(client));

// Rely on Logging Rules
_client.Configuration.SetDefaultMinLogLevel(Exceptionless.Logging.LogLevel.Trace);
}

/// <summary>
/// Initializes a new instance of the <see cref="ExceptionlessLoggerProvider"/> class.
/// </summary>
/// <param name="configure">An <see cref="Action{ExceptionlessConfiguration}"/> which will be used to configure created loggers.</param>
public ExceptionlessLoggerProvider(Action<ExceptionlessConfiguration> configure) {
configure?.Invoke(ExceptionlessClient.Default.Configuration);
_client = ExceptionlessClient.Default;

// Rely on Logging Rules
_client.Configuration.SetDefaultMinLogLevel(Exceptionless.Logging.LogLevel.Trace);

configure?.Invoke(_client.Configuration);
_shouldDispose = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public override void ActivateOptions() {
config.ApiKey = ApiKey;
if (!String.IsNullOrEmpty(ServerUrl))
config.ServerUrl = ServerUrl;

config.UseInMemoryStorage();

// Rely on Logging Rules
config.SetDefaultMinLogLevel(Logging.LogLevel.Trace);
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/Platforms/Exceptionless.Log4net/ExceptionlessAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public override void ActivateOptions() {
config.ApiKey = ApiKey;
if (!String.IsNullOrEmpty(ServerUrl))
config.ServerUrl = ServerUrl;

config.UseInMemoryStorage();

// Rely on Logging Rules
config.SetDefaultMinLogLevel(Logging.LogLevel.Trace);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, L
builder.SetSource(ev.LoggerName);

var props = ev.GetProperties();
foreach (var key in props.GetKeys().Where(key => !_ignoredEventProperties.Contains(key, StringComparer.OrdinalIgnoreCase))) {
foreach (string key in props.GetKeys().Where(key => !_ignoredEventProperties.Contains(key, StringComparer.OrdinalIgnoreCase))) {
string propName = key;
if (propName.StartsWith("log4net:"))
propName = propName.Substring(8);
Expand Down
15 changes: 10 additions & 5 deletions src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@ public ExceptionlessTarget() {
protected override void InitializeTarget() {
base.InitializeTarget();

var apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
var serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent());
string apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
string serverUrl = RenderLogEvent(ServerUrl, LogEventInfo.CreateNullEvent());

if (!String.IsNullOrEmpty(apiKey) || !String.IsNullOrEmpty(serverUrl)) {
_client = new ExceptionlessClient(config => {
if (!String.IsNullOrEmpty(apiKey) && apiKey != "API_KEY_HERE")
config.ApiKey = apiKey;
if (!String.IsNullOrEmpty(serverUrl))
config.ServerUrl = serverUrl;

config.UseLogger(new NLogInternalLoggger());
config.UseInMemoryStorage();
config.SetDefaultMinLogLevel(Logging.LogLevel.Trace); // Rely on NLog Logging Rules

// Rely on Logging Rules
config.SetDefaultMinLogLevel(Logging.LogLevel.Trace);
});
}
else {
} else {
// Rely on Logging Rules
_client.Configuration.SetDefaultMinLogLevel(Logging.LogLevel.Trace);

if (_client.Configuration.Resolver.HasDefaultRegistration<Logging.IExceptionlessLog, Logging.NullExceptionlessLog>()) {
_client.Configuration.UseLogger(new NLogInternalLoggger());
}
Expand Down

0 comments on commit 580eea7

Please sign in to comment.