iSven

Unwichtiges nach Relevanz sortieren...

Howto: Verwenden von log4net in ASP.NET Webanwendungen

Hier ein Kurzleitfaden, um log4net in ASP.NET Webanwendungen zu verwenden:

1. Einträge in der Web.config vornehmen:<!-- code formatted by http://manoli.net/csharpformat/ -->

<style type="text/css">
.csharpcode, .csharpcode pre
{
    font-size: small;
    color: black;
    font-family: Consolas, "Courier New", Courier, Monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt
{
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
}

.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">configSections</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">section</span> <span class="attr">name</span><span class="kwrd">="log4net"</span>
      <span class="attr">type</span><span class="kwrd">="log4net.Config.Log4NetConfigurationSectionHandler, log4net"</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">configSections</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">log4net</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">root</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">level</span> <span class="attr">value</span><span class="kwrd">="DEBUG"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">appender-ref</span> <span class="attr">ref</span><span class="kwrd">="FileAppender"</span><span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">root</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">appender</span> <span class="attr">name</span><span class="kwrd">="FileAppender"</span> <span class="attr">type</span><span class="kwrd">="log4net.Appender.FileAppender"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">file</span> <span class="attr">value</span><span class="kwrd">="log/log-file.txt"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">appendToFile</span> <span class="attr">value</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">layout</span> <span class="attr">type</span><span class="kwrd">="log4net.Layout.PatternLayout"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">conversionPattern</span> <span class="attr">value</span><span class="kwrd">="%d{dd.MM.yy HH:mm:ss} %-5p %c %m%n"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">layout</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">appender</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">log4net</span><span class="kwrd">&gt;</span></pre>

2. In der Global.cs (Global.asax) im Application_Start Event log4net initialisieren:<!-- code formatted by http://manoli.net/csharpformat/ -->

<style type="text/css">
.csharpcode, .csharpcode pre
{
    font-size: small;
    color: black;
    font-family: Consolas, "Courier New", Courier, Monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt
{
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
}

.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode">
log4net.config.xmlconfigurator.configure()</pre>

3. Verwenden des Loggers durch eine statische Variable in jeder Klasse:<!-- code formatted by http://manoli.net/csharpformat/ -->

<style type="text/css">
.csharpcode, .csharpcode pre
{
    font-size: small;
    color: black;
    font-family: Consolas, "Courier New", Courier, Monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt
{
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
}

.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">readonly</span> ILog log = LogManager.GetLogger(<span class="kwrd">typeof</span>(Classname));</pre>

4. Anschliessend kann der Logger in der Klasse verwendet werden:<!-- code formatted by http://manoli.net/csharpformat/ -->

<style type="text/css">
.csharpcode, .csharpcode pre
{
    font-size: small;
    color: black;
    font-family: Consolas, "Courier New", Courier, Monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt
{
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
}

.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode">
log.Debug(<span class="kwrd">string</span>.Format(<span class="str">"LogMeldung {0}"</span>, varname))</pre>

Comments (1) -

  • Frank

    5/7/2008 10:44:19 AM |

    Danke für den Blog Eintrag. Er hat wesentlich zum Verständnis beigetragen.

Comments are closed