Log4Net и дополнительные поля

Двойные кавычки могут быть намного медленнее. Я читал из нескольких мест что, что лучше сделать это

'parse me '.$i.' times'

, чем

"parse me $i times"

, Хотя я сказал бы, что второй дал Вам больше читаемого кода.

12
задан teabot 25 November 2009 в 07:52
поделиться

2 ответа

You could use the "context" feature in log4net. Basically it allows you to set properties that you can then use in your log appender. You can set these properties at different scopes (Global, Thread etc.). In your case I think you could go (for instance, just after the user has logged in):

log4net.ThreadContext.Properties["userid"] = userid;

In your configuration file, you could then use this property and add it to the logging appender. I think it would be something like this for the AdoNetAppender

<parameter>
    <parameterName value="@userid" />
    <dbType value="guid" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{userid}" />
    </layout>
</parameter>

Please note that I have not compiled any of the snippets above, so they might need some tweaking, but it should give you a general idea of how this could be solved.

You can read more about this here.

Ps. I think that the MDC.Set that is referred to in the first answer is obsolete.

20
ответ дан 2 December 2019 в 18:19
поделиться

Я думаю, вам нужно использовать MDC.Set для установки дополнительных значений контекста. Посмотрите Совет № 4 в этом блоге .

2
ответ дан 26 October 2019 в 09:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: