-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add BinaryFormatter auditing EventSource #39874
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
Changes from 1 commit
1ffeffc
3fb9e02
a99f7cf
8b8cc85
6bb4f32
7d53f57
d033375
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Diagnostics.Tracing; | ||
| using System.Threading; | ||
|
|
||
| namespace System.Runtime.Serialization.Formatters.Binary | ||
| { | ||
| [EventSource( | ||
| Name = "System.Runtime.Serialization.Formatters.Binary.BinaryFormatterEventSource", | ||
| LocalizationResources = "FxResources.System.Runtime.Serialization.Formatters.SR")] | ||
| internal sealed class BinaryFormatterEventSource : EventSource | ||
| { | ||
| private const int EventId_SerializationStarted = 10; | ||
| private const int EventId_SerializationEnded = 11; | ||
| private const int EventId_SerializingObject = 12; | ||
| private const int EventId_DeserializationStarted = 20; | ||
| private const int EventId_DeserializationEnded = 21; | ||
| private const int EventId_DeserializingObject = 22; | ||
|
|
||
| // Used to keep track of whether a write operation is in progress. It's | ||
GrabYourPitchforks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // possible the listener itself uses BinaryFormatter to write to a log, | ||
| // and if this is the case we suppress our own logging events so that we | ||
| // enter an infinite recursion scenario. | ||
| private static readonly AsyncLocal<bool> _writeInProgress = new AsyncLocal<bool>(); | ||
GrabYourPitchforks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public static readonly BinaryFormatterEventSource Log = new BinaryFormatterEventSource(); | ||
|
|
||
| private BinaryFormatterEventSource() | ||
| { | ||
| } | ||
|
|
||
| [Event(EventId_SerializationStarted, Opcode = EventOpcode.Start, Keywords = Keywords.Serialization, Level = EventLevel.Informational)] | ||
GrabYourPitchforks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public void SerializationStarted() | ||
GrabYourPitchforks marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (IsEnabled(EventLevel.Informational, Keywords.Serialization) && !_writeInProgress.Value) | ||
|
||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| WriteEvent(EventId_SerializationStarted); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Event(EventId_SerializationEnded, Opcode = EventOpcode.Stop, Keywords = Keywords.Serialization, Level = EventLevel.Informational)] | ||
| public void SerializationEnded() | ||
| { | ||
| if (IsEnabled(EventLevel.Informational, Keywords.Serialization) && !_writeInProgress.Value) | ||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| WriteEvent(EventId_SerializationEnded); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [NonEvent] | ||
| public void SerializingObject(Type type) | ||
| { | ||
| Debug.Assert(type != null); | ||
|
|
||
| if (IsEnabled(EventLevel.Informational, Keywords.Serialization) && !_writeInProgress.Value) | ||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| SerializingObject(type.AssemblyQualifiedName); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Event(EventId_SerializingObject, Keywords = Keywords.Serialization, Level = EventLevel.Informational)] | ||
| private void SerializingObject(string? typeName) | ||
| { | ||
| WriteEvent(EventId_SerializingObject, typeName); | ||
| } | ||
|
|
||
| [Event(EventId_DeserializationStarted, Opcode = EventOpcode.Start, Keywords = Keywords.Deserialization, Level = EventLevel.Informational)] | ||
| public void DeserializationStarted() | ||
| { | ||
| if (IsEnabled(EventLevel.Informational, Keywords.Deserialization) && !_writeInProgress.Value) | ||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| WriteEvent(EventId_DeserializationStarted); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Event(EventId_DeserializationEnded, Opcode = EventOpcode.Stop, Keywords = Keywords.Deserialization, Level = EventLevel.Informational)] | ||
| public void DeserializationEnded() | ||
| { | ||
| if (IsEnabled(EventLevel.Informational, Keywords.Deserialization) && !_writeInProgress.Value) | ||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| WriteEvent(EventId_DeserializationEnded); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [NonEvent] | ||
| public void DeserializingObject(Type type) | ||
| { | ||
| Debug.Assert(type != null); | ||
|
|
||
| if (IsEnabled(EventLevel.Informational, Keywords.Deserialization) && !_writeInProgress.Value) | ||
| { | ||
| try | ||
| { | ||
| _writeInProgress.Value = true; | ||
| DeserializingObject(type.AssemblyQualifiedName); | ||
| } | ||
| finally | ||
| { | ||
| _writeInProgress.Value = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Event(EventId_DeserializingObject, Keywords = Keywords.Deserialization, Level = EventLevel.Informational)] | ||
| private void DeserializingObject(string? typeName) | ||
| { | ||
| WriteEvent(EventId_DeserializingObject, typeName); | ||
| } | ||
|
|
||
| public class Keywords | ||
| { | ||
| public const EventKeywords Serialization = (EventKeywords)1; | ||
| public const EventKeywords Deserialization = (EventKeywords)2; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.