Skip to content

Added structured logging support #184

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

timmyag
Copy link

@timmyag timmyag commented Nov 29, 2020

Im new at this tell me if I've done it wrong.

Have added functionality to handle structured logging, such that the parameters you log against will appear in Greylog as parameters, not just as a conglomerated field.

To use fill in the StructuredLoggingParameterName feild in the config file and assign ${all-event-properties} to a parameter with the same name.



Copy link
Owner

@dustinchilson dustinchilson left a comment

Choose a reason for hiding this comment

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

Overall looks like a good change, going in the right direction.

I'd also recommend adding some tests & updating the readme with your additions.

@@ -8,6 +8,10 @@ internal class GraylogMessageBuilder
private const string NLogLogLevelPropertyName = "nlog_level_name";
private readonly JsonObject _graylogMessage = new JsonObject();
private static readonly DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
private string _structuredLoggingParameterName;

public GraylogMessageBuilder(string structuredLoggingParameterName = null)
Copy link
Owner

Choose a reason for hiding this comment

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

Move this to a builder method. and out of the ctor

Something like WithStructuredLoggingParameter(string parameterName) {...)

var kvPairs = item.Split(new char[] { '=' }, 2);
if (kvPairs.Length == 2)
{
WithCustomProperty(kvPairs[0].Replace(" ", string.Empty), kvPairs[1]);
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need to do any sanitization of the property name per the gelf spec?

@issafram
Copy link
Contributor

Structured logging is already supported with the current library.

Example (injected Microsoft ILogger through NLog)
this.logger.LogError(e, "Issues with userId = {personId}", this.User.PersonId);
In graylog, personId is a searchable field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants