c# - ETW 格式字符串中的转义字符?

标签 c# event-log etw etw-eventsource

我正在使用新的 EventSource 类从我的应用程序写入 Windows 事件日志,到目前为止它很棒。但是,我观察到有两件事会导致可能相关的问题:传递给 Event 属性的格式字符串在写入事件日志之前似乎没有经过正常的 string.Format 处理。

考虑这个 block :

[Event((int)LogEvent.UserCreated, Keywords=Keywords.Directory, Channel=EventChannel.Operational,
Message="Created username {0} with forum post signature {1} and homepage {2}.")]
public void UserCreated(string username, string signature, string homepageAddress)
{
    WriteEvent((int)LogEvent.UserCreated, username, signature, homepageAddress);
}

会发生一些事情:
  • 如果我尝试在消息的格式字符串中插入\n,则事件日志中不会打印换行符。
  • 如果签名字符串包含换行符,则换行符会自然地打印在事件日志中。
  • 如果签名或 homepageAddress 字符串包含类似 ETW 的标记(%1、%2 或 %3),则 ETW 开始将这些伪标记替换为变量本身,并且最终得到嵌套在它们自身中的变量。

  • 我假设如果 ETW 有一个转义字符,它可以用来在格式字符串中添加换行符,并且还允许我预处理我的字符串值以防止这些 sql 注入(inject)样式错误。

    这样的转义字符是否存在?这通常是怎么做的?

    最佳答案

    EventSource list 生成器执行的翻译是

    {0} -> %1
    ...
    {n} -> %(n+1)
    
    &   -> &
    <   -> &lt;
    >   -> &gt;
    '   -> &apos;
    "   -> &quot;
    

    作为引用,转换发生在 string EventProviderBase.TranslateToManifestConvention(string) .

    然后你最终得到消息编译器。 Escapes are as follows :

    %n[!format_specifier!]  Describes an insert. Each insert is an entry in the 
        Arguments array in the FormatMessage function. The value of n can be a number 
        between 1 and 99. The format specifier is optional. If no value is specified, 
        the default is !s!. For information about the format specifier, see wsprintf. 
        The format specifier can use * for either the precision or the width. When 
        specified, they consume inserts numbered n+1 and n+2.
    
    %0  Terminates a message text line without a trailing newline character. This can 
        be used to build a long line or terminate a prompt message without a trailing 
        newline character.
    
    %.  Generates a single period. This can be used to display a period at the 
        beginning of a line, which would otherwise terminate the message text.
    
    %!  Generates a single exclamation point. This can be used to specify an 
        exclamation point immediately after an insert.
    
    %%  Generates a single percent sign.
    
    %n  Generates a hard line break when it occurs at the end of a line. This can be 
        used with FormatMessage to ensure that the message fits a certain width.
    
    %b  Generates a space character. This can be used to ensure an appropriate number 
        of trailing spaces on a line.
    
    %r  Generates a hard carriage return without a trailing newline character.
    

    关于c# - ETW 格式字符串中的转义字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574861/

    相关文章:

    c# - 使用powershell设置事件日志 "Maximum Size"操作

    c# - PageMethods 和 session

    c# - 使用模拟时写入事件日志

    c# - 如何正确地为 XAML 中的数据绑定(bind)添加前缀(或后缀)?

    .net - 在 Windows 上生成 ActivityTracing 事件日志

    c# - 具有相关事件 ID 的 EventSource 跟踪

    windows - 使用 Windows 事件日志的 C++ 项目

    c# - 如何在 Windows IoT 中记录事件?

    c# - 公开基础库中定义的枚举类型

    c# - "Ternary"不同方法签名的运算符