orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Microsoft.Extensions.Logging issue

Open tinpl opened this issue 8 years ago • 6 comments

Seems that from v2 release of Microsoft.Extensions.Logging the way of providing messages templates to logger.Log* functions became more suitable for structured/semantic logging. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x#log-message-template However, Orleans still uses the 'string.Format' style of generating messages. In comparison to M.E.L proposed way of naming variables, it just hides the possibility to implement custom log providers to have more intuitive/straightforward extraction of key/value pairs of important variables. Moreover, in case of line https://github.com/dotnet/orleans/blob/master/src/Orleans.Core/Serialization/SerializationManager.cs#L747 , the old code leads to a bug. Calling this ( case of serializing non-immutable 1dim arrays of primitive types ) throws a System.AggregateException: 'An error occurred while writing to logger(s). (Index (zero based) must be greater than or equal to zero and less than the size of the argument list.)' InnerException {System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at NLog.Extensions.Logging.NLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func 3 formatter) at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func 3 formatter)} System.Exception {System.FormatException}

do not mind the NLog here, had this exceptiong on office pc using regular M.E.L.Console

tinpl avatar Jan 25 '18 19:01 tinpl

This could also be larger issue. One particular thread to dig deeper into actual issues is at https://github.com/aspnet/Logging/issues/612 (and then maybe something like https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage is suitable in some cases). For instance, I'd be interested about how to flow around correlation IDs (and other data).

veikkoeeva avatar Jan 25 '18 19:01 veikkoeeva

@tinpl I'm not sure I fully follow you. SerializationManager line747 uses ILogger.Info which is an extension methods we built on top of ASP.NET logging extension methods: public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args);

when you say

Orleans still uses the 'string.Format' style of generating messages

I understand it as providing a format, and a list of args follows it to fill in the format. And you said this is not suggested by ASP.NET? This is where confuses me, because ASP.NET provided this extension methods to fill in this use case, so I think it is a supported way.

And then you say

it just hides the possibility to implement custom log providers to have more intuitive/straightforward extraction of key/value pairs of important variables

Can you give a more detailed example on how this prevents you "implement custom log providers to have more intuitive/straightforward extraction of key/value pairs of important variables" ? Sorry I might be a little slow on understanding this.

xiazen avatar Jan 26 '18 22:01 xiazen

I understand it as providing a format, and a list of args follows it to fill in the format

And there is a difference because of Microsoft.Extensions.Logging acts not likely String.Format does. I guess it's faster to read it in their doc, so this will bring in some clarity: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x#log-message-template It's quite fine to use it the way as with String.Format, but in some cases (like in SerializationManager:747) if there are fewer args passed into the method than placeholders in format template, the runtime exception will be thrown. Also in the case of https://github.com/dotnet/orleans/blob/master/src/Orleans.Runtime/Catalog/Catalog.cs#L1129, if using String.Format you could expect grainTypeName come first, and activation as a second parameter, but that's not true now.

Can you give a more detailed example on how this prevents you "implement custom log providers to have more intuitive/straightforward extraction of key/value pairs of important variables"

It is also described in the link provided. However, maybe I'm mistaken because not that experienced in how Orleans internally works (using it not for a long time), I can suggest that some metrics or important runtime info could be logged using the way with named variables, so somebody can implement their logging provider and have access to lets say [eventId, List<VariableName, VariableValue>] messages, therefore able to handle them (using variable names) more intuitively, without worrying that you change the message template itself. so let's say message "I did {varname1} with {varname2}" and "We decided to change {varname2}, because strange things happened with {varname1} due to changed {varname3}" will not break user implementations of 'parsing' your logs.

tinpl avatar Jan 26 '18 23:01 tinpl

can we close this issue now after the pr #3916 is merged ? Or is there other places we need to fix ?

xiazen avatar Feb 02 '18 19:02 xiazen

I can suggest that some metrics or important runtime info could be logged using the way with named variables, so somebody can implement their logging provider and have access to lets say [eventId, List<VariableName, VariableValue>] messages, therefore able to handle them (using variable names) more intuitively, without worrying that you change the message template itself. so let's say message "I did {varname1} with {varname2}" and "We decided to change {varname2}, because strange things happened with {varname1} due to changed {varname3}" will not break user implementations of 'parsing' your logs.

I think this use case fits in telemetry more. Build application based on a logging message template seems unstable.

So you are not mistaken . We are all good :)

Thanks for the PR contribution 👍

xiazen avatar Feb 02 '18 19:02 xiazen

@xiazen

can we close this issue now after the pr #3916 is merged ? Or is there other places we need to fix ?

Haven't found any when was going through all occurrences of ILogger usage. Maybe I have missed some because it has taken some time and was quite boring, but it's unlikely... So yeah, seems it could be closed.

tinpl avatar Feb 02 '18 20:02 tinpl