-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
EventSourceEventFormatting - catch FormatException #14697
Changes from all commits
9443af7
58bc086
6c9abf0
e894d59
0b43f0f
f0c520d
adbfa68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core.Diagnostics; | ||
using System; | ||
using System.Diagnostics.Tracing; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
@@ -18,12 +20,24 @@ public static string Format(EventWrittenEventArgs eventData) | |
|
||
if (eventData.Message != null) | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, eventData.Message, payloadArray); | ||
try | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, eventData.Message, payloadArray); | ||
} | ||
catch (FormatException) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should consider adding a line here to call out that the message could not be formatted. If we get to this point, I believe that it indicates a bug in our logging code and it would be helpful if those that encounter this have the awareness and information to open a bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we should also think if we can catch it earlier, some analyzer or test utility possibly. |
||
} | ||
} | ||
|
||
var stringBuilder = new StringBuilder(); | ||
stringBuilder.Append(eventData.EventName); | ||
|
||
if (!string.IsNullOrWhiteSpace(eventData.Message)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we appending the eventData.Message here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If theres a case when you do have a message and you got to this part it means you failed formatting and caught the exception. In this case you still want to print the message and the payload. Just not in a formatted way. |
||
{ | ||
stringBuilder.AppendLine(); | ||
stringBuilder.Append(nameof(eventData.Message)).Append(" = ").Append(eventData.Message); | ||
} | ||
|
||
for (int i = 0; i < eventData.PayloadNames.Count; i++) | ||
{ | ||
stringBuilder.AppendLine(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in some occasions the message inside the eventData is not formattable.
when this happend the program recieves a FormatException in this point.
solution,
catch the format exception and build a message without the formatting.