{
return;
}
+ string message = null;
// See if they want the formatted message
if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.FormattedMessage))
{
- string message = formatter(state, exception);
+ message = formatter(state, exception);
_eventSource.FormattedMessage(
logLevel,
_factoryID,
exceptionJson = ToJson(exceptionInfoData);
}
IReadOnlyList<KeyValuePair<string, string>> arguments = GetProperties(state);
+ message ??= formatter(state, exception);
_eventSource.MessageJson(
logLevel,
_factoryID,
eventId.Id,
eventId.Name,
exceptionJson,
- ToJson(arguments));
+ ToJson(arguments),
+ message);
}
}
}
[Event(5, Keywords = Keywords.JsonMessage, Level = EventLevel.LogAlways)]
- internal unsafe void MessageJson(LogLevel Level, int FactoryID, string LoggerName, int EventId, string EventName, string ExceptionJson, string ArgumentsJson)
+ internal unsafe void MessageJson(LogLevel Level, int FactoryID, string LoggerName, int EventId, string EventName, string ExceptionJson, string ArgumentsJson, string FormattedMessage)
{
if (IsEnabled())
{
EventName ??= "";
ExceptionJson ??= "";
ArgumentsJson ??= "";
+ FormattedMessage ??= "";
fixed (char* loggerName = LoggerName)
fixed (char* eventName = EventName)
fixed (char* exceptionJson = ExceptionJson)
fixed (char* argumentsJson = ArgumentsJson)
+ fixed (char* formattedMessage = FormattedMessage)
{
- const int eventDataCount = 7;
+ const int eventDataCount = 8;
EventData* eventData = stackalloc EventData[eventDataCount];
SetEventData(ref eventData[0], ref Level);
SetEventData(ref eventData[4], ref EventName, eventName);
SetEventData(ref eventData[5], ref ExceptionJson, exceptionJson);
SetEventData(ref eventData[6], ref ArgumentsJson, argumentsJson);
+ SetEventData(ref eventData[7], ref FormattedMessage, formattedMessage);
WriteEventCore(5, eventDataCount, eventData);
}
LogStuff(factory);
- var containsNullEventName = false;
+ bool containsNullEventName = false;
+ bool containsFormattedMessage = false;
foreach (var eventJson in testListener.Events)
{
{
containsNullEventName = true;
}
+ if (eventJson.Contains(@"""FormattedMessage"":""Logger1 Event1 Debug 1"""))
+ {
+ containsFormattedMessage = true;
+ }
}
Assert.True(containsNullEventName, "EventName is supposed to be null but it isn't.");
+ Assert.True(containsFormattedMessage, "FormattedMessage is supposed to be present but it isn't.");
}
}
// Write some MessageJson events with null string.
for (var i = 0; i < 100; i++)
{
- LoggingEventSource.Instance.MessageJson(LogLevel.Trace, 1, "MyLogger", 5, null, null, "testJson");
+ LoggingEventSource.Instance.MessageJson(LogLevel.Trace, 1, "MyLogger", 5, null, null, "testJson", "formattedMessage");
}
bool containsNullEventName = false;
+ bool containsFormattedMessage = false;
foreach (var eventJson in testListener.Events)
{
if (eventJson.Contains(@"""__EVENT_NAME"":""MessageJson""") && eventJson.Contains(@"""EventName"":"""","))
{
containsNullEventName = true;
}
+ if (eventJson.Contains(@"""FormattedMessage"":""formattedMessage"""))
+ {
+ containsFormattedMessage = true;
+ }
}
Assert.True(containsNullEventName, "EventName and ExceptionJson is supposed to be null but it isn't.");
+ Assert.True(containsFormattedMessage, "FormattedMessage is supposed to be present but it isn't.");
}
}