Skip to content

Commit

Permalink
Logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnmbond committed Mar 8, 2024
1 parent 7840eb1 commit 48e9164
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
4 changes: 2 additions & 2 deletions LogicMonitor.Api.Test/Logging/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LoggingTests(ITestOutputHelper iTestOutputHelper) : TestWithOutput(
public async Task WriteLogAsync_WithResourceId_Succeeds()
{
var response = await LogicMonitorClient
.WriteLogAsync(WriteLogLevel.Info, WindowsDeviceId, "Test log message.", default)
.WriteLogAsync(WriteLogLevel.Info, WindowsDeviceId, "Test log message against resource id.", default)
.ConfigureAwait(true);
response.Should().NotBeNull();
}
Expand All @@ -22,7 +22,7 @@ public async Task WriteLogAsync_WithResourceDisplayName_Succeeds()
.ConfigureAwait(true);

var response = await LogicMonitorClient
.WriteLogAsync(WriteLogLevel.Info, windowsDevice.DisplayName, "Test log message.", default)
.WriteLogAsync(WriteLogLevel.Info, windowsDevice.DisplayName, "Test log message against resource display name.", default)
.ConfigureAwait(true);
response.Should().NotBeNull();
}
Expand Down
35 changes: 33 additions & 2 deletions LogicMonitor.Api/Logging/WriteLogRequest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace LogicMonitor.Api.Logging;

/// <summary>
/// A request to write to a log against a resource id
/// A request to write to a log against a resource
/// </summary>
[DataContract]
public class WriteLogRequest : Dictionary<string, object>
Expand Down Expand Up @@ -29,7 +29,7 @@ public WriteLogRequest(WriteLogLevel level, int resourceId, string message)
}

/// <summary>
/// Construct a regular deviceId WriteLogRequest
/// Construct a regular resourceDisplayName WriteLogRequest
/// </summary>
/// <param name="level"></param>
/// <param name="resourceDisplayName"></param>
Expand All @@ -43,6 +43,37 @@ public WriteLogRequest(WriteLogLevel level, string resourceDisplayName, string m
this["message"] = GetPrefix(level) + message;
}

/// <summary>
/// Construct a regular custom property WriteLogRequest
/// </summary>
/// <param name="level"></param>
/// <param name="customPropertyName"></param>
/// <param name="customPropertyValue"></param>
/// <param name="message"></param>
public WriteLogRequest(WriteLogLevel level, string customPropertyName, string customPropertyValue, string message)
{
this["_lm.resourceId"] = new Dictionary<string, string>
{
{ customPropertyName, customPropertyValue }
};
this["message"] = GetPrefix(level) + message;
}

/// <summary>
/// Construct a regular custom property WriteLogRequest
/// </summary>
/// <param name="level"></param>
/// <param name="propertyDictionary">
/// The property dictionary, the combination of which will be used to identify the resource.
/// This must be unique across the specified custom property values for the entire LM portal.
/// </param>
/// <param name="message"></param>
public WriteLogRequest(WriteLogLevel level, Dictionary<string, string> propertyDictionary, string message)
{
this["_lm.resourceId"] = propertyDictionary;
this["message"] = GetPrefix(level) + message;
}

private static string GetPrefix(WriteLogLevel level) => level switch
{
WriteLogLevel.Trace => "[TRCE] ",
Expand Down
44 changes: 41 additions & 3 deletions LogicMonitor.Api/LogicMonitorClient_Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Task<WriteLogResponse> WriteLogAsync(
=> WriteLogAsync(new[] { writeLogRequest }, cancellationToken);

/// <summary>
/// Logs a single writeLogRequest at the specified level
/// Logs a single writeLogRequest
/// </summary>
/// <param name="writeLogLevel"></param>
/// <param name="deviceId">The device id</param>
Expand All @@ -43,10 +43,10 @@ public Task<WriteLogResponse> WriteLogAsync(
=> WriteLogAsync(new[] { new WriteLogRequest(writeLogLevel, deviceId, message) }, cancellationToken);

/// <summary>
/// Logs a single writeLogRequest at the informational level
/// Logs a single writeLogRequest
/// </summary>
/// <param name="writeLogLevel"></param>
/// <param name="deviceDisplayName">The device id</param>
/// <param name="deviceDisplayName">The device display name</param>
/// <param name="message">The message</param>
/// <param name="cancellationToken"></param>
/// <returns>The response</returns>
Expand All @@ -56,4 +56,42 @@ public Task<WriteLogResponse> WriteLogAsync(
string message,
CancellationToken cancellationToken)
=> WriteLogAsync(new[] { new WriteLogRequest(writeLogLevel, deviceDisplayName, message) }, cancellationToken);

/// <summary>
/// Logs a single writeLogRequest
/// </summary>
/// <param name="writeLogLevel"></param>
/// <param name="customPropertyName">The custom property name</param>
/// <param name="customPropertyValue">
/// The custom property value.
/// This must be unique across for the specified custom property value for the entire LM portal.
/// </param>
/// <param name="message">The message</param>
/// <param name="cancellationToken"></param>
/// <returns>The response</returns>
public Task<WriteLogResponse> WriteLogAsync(
WriteLogLevel writeLogLevel,
string customPropertyName,
string customPropertyValue,
string message,
CancellationToken cancellationToken)
=> WriteLogAsync(new[] { new WriteLogRequest(writeLogLevel, customPropertyName, customPropertyValue, message) }, cancellationToken);

/// <summary>
/// Logs a single writeLogRequest
/// </summary>
/// <param name="writeLogLevel"></param>
/// <param name="propertyDictionary">
/// The property dictionary, the combination of which will be used to identify the resource.
/// This must be unique across the specified custom property values for the entire LM portal.
/// </param>
/// <param name="message">The message</param>
/// <param name="cancellationToken"></param>
/// <returns>The response</returns>
public Task<WriteLogResponse> WriteLogAsync(
WriteLogLevel writeLogLevel,
Dictionary<string, string> propertyDictionary,
string message,
CancellationToken cancellationToken)
=> WriteLogAsync(new[] { new WriteLogRequest(writeLogLevel, propertyDictionary, message) }, cancellationToken);
}

0 comments on commit 48e9164

Please sign in to comment.