New NLog features

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

New NLog features

Jarek Kowalski
Administrator
Hi!

The new NLog snapshot has been released. It adds some new features which
exploit the recently added Wrapper targets.

1. The first one is ASPNetBufferingWrapper which is a specialized buffering
wrapper which works on ASP.NET requests. All log events generated during the
execution of the HTTP Request in ASP.NET are gathered in a buffer. As soon
as the request finishes, the log events are sent down to the wrapped
targets. It means, you can, for example, send the trace of a HTTP request in
a single email and different sessions won't interfere.

To use this feature you need to add the <httpModules> entry in your
Web.config:

<configuration>
    <system.web>
        <httpModules>
            <add name="NLog" type="NLog.Web.NLogHttpModule, NLog"/>
        </httpModules>
    </system.web>
</configuration>

If you don't do this - ASPNetBufferingWrapper will not work.

2. Another wrapper is PostFilteringWrapper which operates on buffers (you
should always wrap it around the buffering wrapper like BufferingTarget,
AsyncTarget or ASPNetBuffering target) and lets you filter the buffers with
the filtering condition evaluated based on the entire buffer. The typical
use case is this:

    * we want all HTTP Requests to only include messages at the Info level
    * should there be any Warn, Error or Fatal during the request
processing, we want to dump DETAILED log (Debug level and above)
    * should there be any Fatal errors, we want to dump VERY DETAILED log
(Trace level and above)

This is very easy using the new wrapper:
===========================================================
<?xml version="1.0" encoding="windows-1250" ?>
<nlog autoReload="true" internalLogLevel="Trace">
    <targets>
        <target name="userlog" type="ASPNetBufferingWrapper">
            <target type="PostFilteringWrapper" defaultFilter="(level >=
LogLevel.Info)">
                <when exists="level >= LogLevel.Fatal" filter="level >=
LogLevel.Trace" />
                <when exists="level >= LogLevel.Warn" filter="level >=
LogLevel.Debug" />

                <target name="file" type="File"
fileName="${basedir}/../logs/${shortdate}.log" />
            </target>
        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Trace" appendTo="userlog" />
    </rules>
</nlog>

===========================================================

3.The filter expressions in the above example are written in a special
mini-language which has also been recently introduced in NLog. Some
documentation is available in the help file. Below are some most important
features:

The language consists of:

* relational operators (==, !=, <=, <=, >= and >)
* and, or, not boolean operators
* string literals which are always evaluated as layouts - '${somerenderer}'
* boolean literals - true and false
* numeric literals - 12345 (integer literal) and 12345.678 (floating point
literal)
* log level literals - LogLevel.Trace, LogLevel.Debug, ... LogLevel.Fatal
* predefined keywords to access the most common log event properties -
level, message and logger
* braces - to override default priorities and group expressions together

Examples

Here are some examples of conditions:

level > LogLevel.Debug - matches the messages whose level is greater than
Debug
(level > LogLevel.Debug) or contains(message,'xxx') - matches the messages
whose level is greater than Debug or which include the xxx substring in the
log message
starts-with(logger,'Kopytko.') - matches the loggers whose names start with
Kopytko.
ends-with(logger,'.SQL') or ends-with(logger,'.XML') - matches the loggers
whose names end with either .SQL or .XML
true - matches everything
false - matches nothing
length(message) > 100 - matches the log events where the length of the log
message is greater than 100
'${shortdate}' == '2005-11-10' - matches on the specified date

Available functions

The following functions are available:

contains(s1,s2)
Determines whether the second string is a substring of the first one.
Returns: true when the second string is a substring of the first string,
false otherwise.

ends-with(s1,s2)
Determines whether the second string is a suffix of the first one. Returns:
true when the second string is a prefix of the first string, false
otherwise.

equals(o1,o2)
Compares two objects for equality.
Returns: true when two objects are equal, false otherwise.

length(s)
Returns the length of a string.
Returns: The length of a string.

starts-with(s1,s2)
Determines whether the second string is a prefix of the first one. Returns:
true when the second string is a prefix of the first string, false
otherwise.

New functions are ultra-easy to add, just create an ordinary function and
mark it with [ConditionMethod] attribute. See this example:

http://trac.sav.net/nlog/file/trunk/NLog/src/NLog/Conditions/ConditionMethods.cs

4. There's a new <when condition="..." action="..." /> filter which lets you
specify filters using the condition language:

<rules>
    <logger name="*" writeTo="file">
        <filters>
            <when condition="(level >= LogLevel.Debug and
contains(message,'Kopytko')) or level==LogLevel.Warn" action="Ignore" />
        </filters>
    </logger>
</rules>

Comments are - as always - welcome.

BTW. The server which hosts TRAC and SVN has been moved to the new
colocation facility after a crash. Things should be more stable than before,
but in case of any problems - report them here. Can you send me your ping
times to trac.sav.net?

--
Jaroslaw Kowalski
http://blog.jkowalski.net/ 



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Jarek Kowalski
Administrator
I forgot - the snapshots are (as usual) here:

http://nlog.sourceforge.net/snapshots/

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Ron Grabowski
In reply to this post by Jarek Kowalski
How is ASPNetRequestBufferingWrapper different from
ASPNetBufferingWrapper? I haven't looked at the code for
ASPNetRequestBufferingWrapper. I wondered how you were handling the
case of the request being aborted yet the messages still being
processed. The HttpModule approach makes sense. Did
ASPNetRequestBufferingWrapper implement IHttpModule?

--- Jaroslaw Kowalski <[hidden email]> wrote:

> Hi!
>
> The new NLog snapshot has been released. It adds some new features
> which
> exploit the recently added Wrapper targets.
>
> 1. The first one is ASPNetBufferingWrapper which is a specialized
> buffering
> wrapper which works on ASP.NET requests. All log events generated
> during the
> execution of the HTTP Request in ASP.NET are gathered in a buffer. As
> soon
> as the request finishes, the log events are sent down to the wrapped
> targets. It means, you can, for example, send the trace of a HTTP
> request in
> a single email and different sessions won't interfere.
>
> To use this feature you need to add the <httpModules> entry in your
> Web.config:
>
> <configuration>
>     <system.web>
>         <httpModules>
>             <add name="NLog" type="NLog.Web.NLogHttpModule, NLog"/>
>         </httpModules>
>     </system.web>
> </configuration>
>
> If you don't do this - ASPNetBufferingWrapper will not work.


-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Ron Grabowski
In reply to this post by Jarek Kowalski
This is neat:

 // ASPNetBufferingTargetWrapper.cs
 public override void Initialize()
 {
  NLog.Web.NLogHttpModule.BeginRequest +=
   new EventHandler(this.OnBeginRequest);
  NLog.Web.NLogHttpModule.EndRequest +=
   new EventHandler(this.OnEndRequest);
 }

I've never thought about the concept of having a very simple HttpModule
that delegates events to subscribers. This allows Targets easy access
to the BeginRequest and EndRequest without cluttering up the web.config
with additional HttpModules.

--- Jaroslaw Kowalski <[hidden email]> wrote:

> Hi!
>
> The new NLog snapshot has been released. It adds some new features
> which
> exploit the recently added Wrapper targets.
>
> 1. The first one is ASPNetBufferingWrapper which is a specialized
> buffering
> wrapper which works on ASP.NET requests. All log events generated
> during the
> execution of the HTTP Request in ASP.NET are gathered in a buffer. As
> soon
> as the request finishes, the log events are sent down to the wrapped
> targets. It means, you can, for example, send the trace of a HTTP
> request in
> a single email and different sessions won't interfere.
>
> To use this feature you need to add the <httpModules> entry in your
> Web.config:
>
> <configuration>
>     <system.web>
>         <httpModules>
>             <add name="NLog" type="NLog.Web.NLogHttpModule, NLog"/>
>         </httpModules>
>     </system.web>
> </configuration>
>
> If you don't do this - ASPNetBufferingWrapper will not work.
>
> 2. Another wrapper is PostFilteringWrapper which operates on buffers
> (you
> should always wrap it around the buffering wrapper like
> BufferingTarget,
> AsyncTarget or ASPNetBuffering target) and lets you filter the
> buffers with
> the filtering condition evaluated based on the entire buffer. The
> typical
> use case is this:
>
>     * we want all HTTP Requests to only include messages at the Info
> level
>     * should there be any Warn, Error or Fatal during the request
> processing, we want to dump DETAILED log (Debug level and above)
>     * should there be any Fatal errors, we want to dump VERY DETAILED
> log
> (Trace level and above)
>
> This is very easy using the new wrapper:
> ===========================================================
> <?xml version="1.0" encoding="windows-1250" ?>
> <nlog autoReload="true" internalLogLevel="Trace">
>     <targets>
>         <target name="userlog" type="ASPNetBufferingWrapper">
>             <target type="PostFilteringWrapper" defaultFilter="(level
> >=
> LogLevel.Info)">
>                 <when exists="level >= LogLevel.Fatal" filter="level
> >=
> LogLevel.Trace" />
>                 <when exists="level >= LogLevel.Warn" filter="level
> >=
> LogLevel.Debug" />
>
>                 <target name="file" type="File"
> fileName="${basedir}/../logs/${shortdate}.log" />
>             </target>
>         </target>
>     </targets>
>
>     <rules>
>         <logger name="*" minlevel="Trace" appendTo="userlog" />
>     </rules>
> </nlog>
>
> ===========================================================
>
> 3.The filter expressions in the above example are written in a
> special
> mini-language which has also been recently introduced in NLog. Some
> documentation is available in the help file. Below are some most
> important
> features:
>
> The language consists of:
>
> * relational operators (==, !=, <=, <=, >= and >)
> * and, or, not boolean operators
> * string literals which are always evaluated as layouts -
> '${somerenderer}'
> * boolean literals - true and false
> * numeric literals - 12345 (integer literal) and 12345.678 (floating
> point
> literal)
> * log level literals - LogLevel.Trace, LogLevel.Debug, ...
> LogLevel.Fatal
> * predefined keywords to access the most common log event properties
> -
> level, message and logger
> * braces - to override default priorities and group expressions
> together
>
> Examples
>
> Here are some examples of conditions:
>
> level > LogLevel.Debug - matches the messages whose level is greater
> than
> Debug
> (level > LogLevel.Debug) or contains(message,'xxx') - matches the
> messages
> whose level is greater than Debug or which include the xxx substring
> in the
> log message
> starts-with(logger,'Kopytko.') - matches the loggers whose names
> start with
> Kopytko.
> ends-with(logger,'.SQL') or ends-with(logger,'.XML') - matches the
> loggers
> whose names end with either .SQL or .XML
> true - matches everything
> false - matches nothing
> length(message) > 100 - matches the log events where the length of
> the log
> message is greater than 100
> '${shortdate}' == '2005-11-10' - matches on the specified date
>
> Available functions
>
> The following functions are available:
>
> contains(s1,s2)
> Determines whether the second string is a substring of the first one.
>
> Returns: true when the second string is a substring of the first
> string,
> false otherwise.
>
> ends-with(s1,s2)
> Determines whether the second string is a suffix of the first one.
> Returns:
> true when the second string is a prefix of the first string, false
> otherwise.
>
> equals(o1,o2)
> Compares two objects for equality.
> Returns: true when two objects are equal, false otherwise.
>
> length(s)
> Returns the length of a string.
> Returns: The length of a string.
>
> starts-with(s1,s2)
> Determines whether the second string is a prefix of the first one.
> Returns:
> true when the second string is a prefix of the first string, false
> otherwise.
>
> New functions are ultra-easy to add, just create an ordinary function
> and
> mark it with [ConditionMethod] attribute. See this example:
>
>
http://trac.sav.net/nlog/file/trunk/NLog/src/NLog/Conditions/ConditionMethods.cs

>
> 4. There's a new <when condition="..." action="..." /> filter which
> lets you
> specify filters using the condition language:
>
> <rules>
>     <logger name="*" writeTo="file">
>         <filters>
>             <when condition="(level >= LogLevel.Debug and
> contains(message,'Kopytko')) or level==LogLevel.Warn" action="Ignore"
> />
>         </filters>
>     </logger>
> </rules>
>
> Comments are - as always - welcome.
>
> BTW. The server which hosts TRAC and SVN has been moved to the new
> colocation facility after a crash. Things should be more stable than
> before,
> but in case of any problems - report them here. Can you send me your
> ping
> times to trac.sav.net?
>
> --
> Jaroslaw Kowalski
> http://blog.jkowalski.net/ 
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the JBoss Inc.
> Get Certified Today * Register for a JBoss Training Course
> Free Certification Exam for All Training Attendees Through End of
> 2005
> Visit http://www.jboss.com/services/certification for more
> information
> _______________________________________________
>
=== message truncated ===



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Jarek Kowalski
Administrator
In reply to this post by Ron Grabowski
> How is ASPNetRequestBufferingWrapper different from
> ASPNetBufferingWrapper? I haven't looked at the code for
> ASPNetRequestBufferingWrapper. I wondered how you were handling the
> case of the request being aborted yet the messages still being
> processed. The HttpModule approach makes sense. Did
> ASPNetRequestBufferingWrapper implement IHttpModule?

That ASPNetRequestBufferingWrapper was just a work-in-progress and it didn't
actually work. No it didn't implement IHttpModule.
The new one doesn't, either. The module is implemented in
NLog.Web.NLogHttpWrapper. It captures BeginRequest() and EndRequest()
handlers and exposes them to targets as another event chains.

ASPNetBufferingTargetWrapper registers for these events. OnBeginRequest
allocates a new buffer and OnEndRequest flushes it. Simple.

Jarek



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Ron Grabowski
In reply to this post by Jarek Kowalski
How should people encode the less than and/or double quote characters?
Does the parser understand &lt; and whatever the escape code is for " ?

--- Jaroslaw Kowalski <[hidden email]> wrote:

> <when exists="level >= LogLevel.Fatal" filter="level



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: New NLog features

Jarek Kowalski
Administrator
Standard XML entities apply:

<   becomes &lt;
" becomes &quot;
' becomes &apos;
> becomes &gt;
& becomes &amp;

For some reason you can use > in attribute values without quoting.

I know that quoting looks ugly, alternatively we could add "lt", "gt", "ge",
"le", "eq", "ne" infix operators or predicate functions:

a < b
a lt b
less-than(a,b)

a > b
a gt b
greater-than(a,b)

a >= b
a ge b
greater-than-or-equal(a,b)

a <= b
a le b
less-than-or-equal(a,b)

a == b
a eq b
equals(a,b)

a != b
a ne b
!equals(a,b)

But I'm not sure it's worth it.

--
Jaroslaw Kowalski
http://blog.jkowalski.net/
----- Original Message -----
From: "Ron Grabowski" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, October 25, 2005 10:30 PM
Subject: Re: [Nlog-list] New NLog features


> How should people encode the less than and/or double quote characters?
> Does the parser understand &lt; and whatever the escape code is for " ?
>
> --- Jaroslaw Kowalski <[hidden email]> wrote:
>
>> <when exists="level >= LogLevel.Fatal" filter="level
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the JBoss Inc.
> Get Certified Today * Register for a JBoss Training Course
> Free Certification Exam for All Training Attendees Through End of 2005
> Visit http://www.jboss.com/services/certification for more information
> _______________________________________________
> Nlog-list mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/nlog-list
>



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: New NLog features

Francis1
In reply to this post by Jarek Kowalski
Very nice Jaroslaw!  The possibilities are limitless with these wrappers.

Keep up the good work,

Francis



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.
Get Certified Today * Register for a JBoss Training Course
Free Certification Exam for All Training Attendees Through End of 2005
Visit http://www.jboss.com/services/certification for more information
_______________________________________________
Nlog-list mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/nlog-list
Loading...