1

I tried to log ip address of the incoming request. I tried following configuration but it does not working for me its log in as "DEBUG".

Global.aspx

    void Application_BeginRequest(object sender, EventArgs e)
{
    log4net.ThreadContext.Properties["addr"] = Request.UserHostAddress;
}   

RollingFileAppender

        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%p{addr} %date [%thread] %-5level %logger - %message%newline"/>
        </layout>

Log look like this

DEBUG 2018-11-20 06:22:50,328 [40] DEBUG Conference - loging successfully

1 Answers1

1

Lowercase %p is a shortcut for %level.

To output a custom property, you should use uppercase %P

<layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%P{addr} %date [%thread] %-5level %logger - %message%newline"/>
</layout>

or the full keyword %property

<layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%property{addr} %date [%thread] %-5level %logger - %message%newline"/>
</layout>

More info at the Log4net reference.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • Hi, As i am using BeginRequest method in global asax file to log the ip address of the incoming request. However its seems not working perfectly, because when multiple users interact with application and then different ip addresses logged in the file for same users. void Application_BeginRequest(object sender, EventArgs e) { log4net.ThreadContext.Properties["addr"] = Request.UserHostAddress; } – Shahid Majeed Nov 29 '18 at 21:01
  • 85.24.130.107 2018-11-29 09:55:44,061 [44] DEBUG Conference - Gustav: Debit Token: 1688478159 ddReservations_SelectedIndexChanged end 213.136.59.97 2018-11-29 09:55:47,343 [9] DEBUG Conference - Gustav: Debit Token: 1688478159 btnSave_Click start – Shahid Majeed Nov 29 '18 at 21:02
  • Can't explain, but is not related to the `%P` marker, but to the value that gets assigned to the `addr` entry via `Request.UserHostAddress`. Stuff for an other question. – pfx Nov 29 '18 at 21:08
  • Try to assign to [`LogicalThreadContext`](https://logging.apache.org/log4net/log4net-1.2.12/release/sdk/log4net.LogicalThreadContext.html) via `LogicalThreadContext.Properties["addr"] = Request.UserHostAddress`. Might be a thead reuse side effect. – pfx Nov 29 '18 at 21:18
  • i think different thread create problem now if multiple threads created for same user and ip for some threads are null. have a look – Shahid Majeed Nov 30 '18 at 09:42
  • 213.136.59.97 2018-11-30 07:35:33,561 [14] DEBUG Conference - Kitty: visaBoka LoadPrintTimeline end: (null) 2018-11-30 07:36:37,968 [32] DEBUG Conference - Kitty: Statistik btnVisa_Click started – Shahid Majeed Nov 30 '18 at 09:42
  • Interesting find. Can you open a new question? It will be `log4net.LogicalThreadContext`/`ThreadContext` related, not `%P`. – pfx Nov 30 '18 at 09:48
  • new question created. I mistakenly create answer instead of question. – Shahid Majeed Nov 30 '18 at 10:02
  • hi pfx, have you figureout what will the problem i tried myself but did not succeed yet. – Shahid Majeed Dec 04 '18 at 12:37
  • I noticed your other question about this, but can only advice to use **LogicalThreadContext** instead; `log4net.LogicalThreadContext.Properties["addr"] = Request.UserHostAddress;`. Similar questions have this marked as an answer. – pfx Dec 04 '18 at 13:03