ASPNetBufferingTargetWrapper: how to only record messages from authenciated users?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ASPNetBufferingTargetWrapper: how to only record messages from authenciated users?

Ron Grabowski
Suppose I want to use the ASPNetBufferingTargetWrapper to log messages generated from authenticated users only. Is there a more effecient way other than having to check every messages as it is being written?

public class AuthenticatedUsersASPNetBufferingTargetWrapper : ASPNetBufferingTargetWrapper
{
 protected internal override void Write(LogEventInfo logEvent)
 {
   if (HttpContext.Current.User.Identity.IsAuthenticated)
   {
    base.Write(logEvent);
   }
 }
}

Is it possible to do a single test on IsAuthenticated and turn off logging for the request if the user is not authenticated:

if (HttpContext.Current.User.Identity.IsAuthenticated)
{
 base.Write(logEvent);
}
else
{
 disableTargetForCurrentThread(); // ???
}

I wonder what should happen if the user becomes authenticated during the course of the request (i.e. they login and are Server.Transfered to a page that generates additional messages).

- Ron





-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
Reply | Threaded
Open this post in threaded view
|

Re: ASPNetBufferingTargetWrapper: how to only record messages from authenciated users?

Jarek Kowalski
Administrator
Hi Ron,

You can get similar functionality by using FilteringWrapper target and a
custom condition function:

<target name="aaa" type="FilteringWrapper"
condition="IsHTTPUserAuthenticated()">
    <target type="ASPNetBufferingWrapper">
        <target ... />
    </target>
</target>

IsHTTPUserAuthenticated needs to be a static function returning bool marked
with [ConditionMethod] placed in a class marked with [ConditionMethods]
attribute.

==============
[ConditionMethods]
public class NLogCustomConditions
{
    [ConditionMethod("IsHTTPUserAuthenticated")]
    public static bool IsHTTPUserAuthenticated()
    {
       if (HttpContext.Current == null)
            return false;

        return HttpContext.Current.User.Identity.IsAuthenticated;
    }
}
==============

You must register your condition method, either programmatically using one
of the methods of ConditionMethodFactory class or declaratively using the
standard <extensions><add /></extensions> method.

Jarek

----- Original Message -----
From: "Ron Grabowski" <[hidden email]>
To: <[hidden email]>
Sent: Sunday, January 01, 2006 11:42 PM
Subject: [Nlog-list] ASPNetBufferingTargetWrapper: how to only record
messages from authenciated users?


> Suppose I want to use the ASPNetBufferingTargetWrapper to log messages
> generated from authenticated users only. Is there a more effecient way
> other than having to check every messages as it is being written?
>
> public class AuthenticatedUsersASPNetBufferingTargetWrapper :
> ASPNetBufferingTargetWrapper
> {
> protected internal override void Write(LogEventInfo logEvent)
> {
>   if (HttpContext.Current.User.Identity.IsAuthenticated)
>   {
>    base.Write(logEvent);
>   }
> }
> }
>
> Is it possible to do a single test on IsAuthenticated and turn off logging
> for the request if the user is not authenticated:
>
> if (HttpContext.Current.User.Identity.IsAuthenticated)
> {
> base.Write(logEvent);
> }
> else
> {
> disableTargetForCurrentThread(); // ???
> }
>
> I wonder what should happen if the user becomes authenticated during the
> course of the request (i.e. they login and are Server.Transfered to a page
> that generates additional messages).
>
> - Ron
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Nlog-list mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/nlog-list
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: ASPNetBufferingTargetWrapper: how to only record messages from authenciated users?

Jarek Kowalski
Administrator
Hi Ron,

One more thing. I've just committed two new layout renderers:

${aspnet-user-identity} - HttpContext.Current.User.Identity.Name

${aspnet-user-authtype}
    if (HttpContext.Current.User.Identity.IsAuthenticated)
        HttpContext.Current.User.Identity.AuthenticationType
    else
        ""

You can use these in a condition too:

<target name="aaa" type="FilteringWrapper" condition="
length('${aspnet-user-authtype}') != 0">
    <target type="ASPNetBufferingWrapper">
        <target ... />
    </target>
 </target>

Meaning:

If the ${aspnet-user-authtype} returns a string of non-zero length, pass the
log event to the buffering wrapper.

Jarek

----- Original Message -----
From: "Jaroslaw Kowalski" <[hidden email]>
To: "Ron Grabowski" <[hidden email]>;
<[hidden email]>
Sent: Sunday, January 01, 2006 11:59 PM
Subject: Re: [Nlog-list] ASPNetBufferingTargetWrapper: how to only record
messages from authenciated users?


> Hi Ron,
>
> You can get similar functionality by using FilteringWrapper target and a
> custom condition function:
>
> <target name="aaa" type="FilteringWrapper"
> condition="IsHTTPUserAuthenticated()">
>    <target type="ASPNetBufferingWrapper">
>        <target ... />
>    </target>
> </target>
>
> IsHTTPUserAuthenticated needs to be a static function returning bool
> marked with [ConditionMethod] placed in a class marked with
> [ConditionMethods] attribute.
>
> ==============
> [ConditionMethods]
> public class NLogCustomConditions
> {
>    [ConditionMethod("IsHTTPUserAuthenticated")]
>    public static bool IsHTTPUserAuthenticated()
>    {
>       if (HttpContext.Current == null)
>            return false;
>
>        return HttpContext.Current.User.Identity.IsAuthenticated;
>    }
> }
> ==============
>
> You must register your condition method, either programmatically using one
> of the methods of ConditionMethodFactory class or declaratively using the
> standard <extensions><add /></extensions> method.
>
> Jarek
>
> ----- Original Message -----
> From: "Ron Grabowski" <[hidden email]>
> To: <[hidden email]>
> Sent: Sunday, January 01, 2006 11:42 PM
> Subject: [Nlog-list] ASPNetBufferingTargetWrapper: how to only record
> messages from authenciated users?
>
>
>> Suppose I want to use the ASPNetBufferingTargetWrapper to log messages
>> generated from authenticated users only. Is there a more effecient way
>> other than having to check every messages as it is being written?
>>
>> public class AuthenticatedUsersASPNetBufferingTargetWrapper :
>> ASPNetBufferingTargetWrapper
>> {
>> protected internal override void Write(LogEventInfo logEvent)
>> {
>>   if (HttpContext.Current.User.Identity.IsAuthenticated)
>>   {
>>    base.Write(logEvent);
>>   }
>> }
>> }
>>
>> Is it possible to do a single test on IsAuthenticated and turn off
>> logging for the request if the user is not authenticated:
>>
>> if (HttpContext.Current.User.Identity.IsAuthenticated)
>> {
>> base.Write(logEvent);
>> }
>> else
>> {
>> disableTargetForCurrentThread(); // ???
>> }
>>
>> I wonder what should happen if the user becomes authenticated during the
>> course of the request (i.e. they login and are Server.Transfered to a
>> page that generates additional messages).
>>
>> - Ron
>>
>>
>>
>>
>>
>> -------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
>> files
>> for problems?  Stop!  Download the new AJAX search engine that makes
>> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
>> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
>> _______________________________________________
>> Nlog-list mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/nlog-list
>>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Nlog-list mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/nlog-list
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
NLog Blog