Howto: Verwenden von log4net in ASP.NET Webanwendungen

7. Februar 2007

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

1. Einträge in der Web.config vornehmen:



<configSections>
  <section name="log4net"
      type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

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

2. In der Global.cs (Global.asax) im Application_Start Event log4net initialisieren:



log4net.config.xmlconfigurator.configure()

3. Verwenden des Loggers durch eine statische Variable in jeder Klasse:



private static readonly ILog log = LogManager.GetLogger(typeof(Classname));

4. Anschliessend kann der Logger in der Klasse verwendet werden:



log.Debug(string.Format("LogMeldung {0}", varname))

.NET, ASP.NET

.NET 3.0 ist da!

8. November 2006
Nach vielen CTP's und 2 Betas ist die Version Nummer 3 des .NET Frameworks released worden. Allerdings hat sich bei den bestehenden .NET Komponenten nichts geändert, sondern es sind lediglich neue aber sehr interessante Komponenten hinzugekommen:
  • Windows Presentation Foundation
  • Windows Communication Foundation
  • Windows Workflow Foundation
Insofern wäre die Versionsnummer 2.5 für das Framework eigentlich eher angebracht gewesen, denn bei den Sprachelementen bleibt alles wie gehabt.

Mehr Infos und Links zum Download

Development, .NET

Visual Studio 2005 Performance Probleme bei ASP.NET Projekten

17. Oktober 2006
Wenn's mal wieder länger dauert beim Build: Prüfen, ob Verweise bzw. eingebundene Assemblies mit einer Refresh Datei (wie Assembly.dll.refresh) versehen sind. Diese Datei sorgt bei jedem Build dafür, dass geprüft wird, ob einen neue Version der Assembly vorliegt und das kann den Buildprozess extrem ausbremsen. In einem Projekt von mir benötigte ein Build mit einer Vielzahl von Refresh Dateien so ca. 2 Minuten, nach dem Löschen der Refresh Dateien waren es nur noch 10 Sekunden.
Das Löschen ist recht gefahrlos. Man muss dann nur selbst dafür sorgen, dass die Assemblies immer aktuell sind.

Development, .NET, ASP.NET

Windows Workflow Foundation

25. Juli 2006
Auf der Suche nach Tipps und Tricks zu der Windows Workfow Foundation bin ich auf folgende Blogs gestossen:

Advanced Workflow: Enabling Tricky Scenarios:

blogs.msdn.com/sonalic/

Paul Andrew

Development, .NET

.NET 3.0

24. Juli 2006
Aus WinFX wird .NET 3.0! Nachdem erst im November 2005 .NET 2.0 erschienen ist, wird einem ganz schwindlig, bei dem Gedanken, dass das Framework schon wieder einen Nummer höher dreht. Viele haben doch erst gerade begonnen mit 2.0 zu arbeiten... Allerdings sollte man sich nicht aus der Ruhe bringen lassen, denn in .NET 3.0 enthält nur die ehemaligen WinFX Projekte, als da wären:
  • Windows Communication Foundation
  • Wiindows Presentation Foundation
  • Windows Workflow Foundation
  • Windows Cardspace
Die frischesten Infos und Samples zu .NET 3.0 scheint es übrigens hier zu geben: Microsoft .NET Framework 3.0 Community (NetFx3)

.NET

Modales Popup in ASP.NET

24. Juli 2006

Ein modales Popup in ASP.NET zu erzeugen ist nicht schwer, aber umständlich. Leider! Hier eine kleine Anleitung:

Der erste Schritt ist ein Javascript zum Öffnen eines Popups:

function openPopup(url, title) {
  width = 860;
  height = 360;
  posx = window.screenLeft + posx;
  posy = window.screenTop + posy;

  var Fenster;
    
  if (window.showModalDialog){
    var features = "dialogWidth:" + width + "px;dialogHeight:" + height +
      "px;resizable:no;scroll:no;status:no;edge:sunken;help:no;center:yes";
    Fenster = window.showModalDialog(url, title, features);
  } else {
    var features = "locationbar=no, directories=no, status=no, " +
      "menubar=no, scrollbars=no, resizable=no, modal=yes, dependent=yes, width=" +
      width + ",height=" + height + ",left=" + posx + ",top=" + posy;
    Fenster = window.open( url, title, features);
    Fenster.focus();
  }
}

 

Damit die Inhalte des Popups auch korrekt angezeigt werden (werden sonst gecacht), muss das Page_Load des Popups angepasst werden:

protected void Page_Load(object sender, EventArgs e) {
  if (!this.IsPostBack) {
    Response.Expires = 0;
    Response.Cache.SetNoStore();
    Response.AppendHeader("Pragma", "no-cache");
  }
}

.NET, ASP.NET

Hands-On Lab für MSBuild

4. April 2006

Mit .NET 2.0 hat Microsoft den Buildprocess von Visual Studio angepasst und sich mal wieder ein wenig an etablierten Methoden der Java Welt bedient (Ant).

Wem bisher allerdings kein gutes Tutorial in die Hände gekommen ist, so wie mir, sollte sich dies mal näher ansehen.

Development, .NET

PatternLayout in log4net

4. April 2006

Für das .NET Logging-Framework log4net habe ich immer wieder nach den Konfigurationseinstellungen der PatternLayouts, wie also die Ausgabe formatiert wird, gesucht. Damit die Suche endlich ein Ende hat:

Conversion Character Effect
a Used to output the frienly name of the AppDomain where the logging event was generated.
c

Used to output the logger of the logging event. The logger conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

If a precision specifier is given, then only the corresponding number of right most components of the logger name will be printed. By default the logger name is printed in full.

For example, for the logger name "a.b.c" the pattern %c{2} will output "b.c".

C

Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form.

For example, for the class name "log4net.Layout.PatternLayout", the pattern %C{1} will output "PatternLayout".

WARNING Generating the caller class information is slow. Thus, it's use should be avoided unless execution speed is not an issue.

d

Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,fff} or %d{dd MMM yyyy HH:mm:ss,fff}. If no date format specifier is given then ISO8601 format is assumed (ISO8601DateFormatter).

The date format specifier admits the same syntax as the time pattern string of the ToString.

For better results it is recommended to use the log4net date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormatter, and respectively ISO8601DateFormatter. For example, %d{ISO8601} or %d{ABSOLUTE}.

These dedicated date formatters perform significantly better than ToString.

F

Used to output the file name where the logging request was issued.

WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.

l

Used to output location information of the caller which generated the logging event.

The location information depends on the CLI implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses.

The location information can be very useful. However, it's generation is extremely slow. It's use should be avoided unless execution speed is not an issue.

L

Used to output the line number from where the logging request was issued.

WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.

m

Used to output the application supplied message associated with the logging event.

M

Used to output the method name where the logging request was issued.

WARNING Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.

n

Outputs the platform dependent line separator character or characters.

This conversion character offers practically the same performance as using non-portable line separator strings such as "\n", or "\r\n". Thus, it is the preferred way of specifying a line separator.

p

Used to output the level of the logging event.

P

Used to output the an event specific property. The key to lookup must be specified within braces and directly following the pattern specifier, e.g. %X{user} would include the value from the property that is keyed by the string 'user'. Each property value that is to be included in the log must be specified separately. Properties are added to events by loggers or appenders. By default no properties are defined.

r

Used to output the number of milliseconds elapsed since the start of the application until the creation of the logging event.

t

Used to output the name of the thread that generated the logging event. Uses the thread number if no name is available.

u

Used to output the user name for the currently active user (Principal.Identity.Name).

WARNING Generating caller information is extremely slow. It's use should be avoided unless execution speed is not an issue.

W

Used to output the WindowsIdentity for the currently active user.

WARNING Generating caller WindowsIdentity information is extremely slow. It's use should be avoided unless execution speed is not an issue.

x

Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.

X

Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The key to lookup must be specified within braces and directly following the pattern specifier, e.g. %X{user} would include the value from the MDC that is keyed by the string 'user'. Each MDC value that is to be included in the log must be specified separately.

%

The sequence %% outputs a single percent sign.

Development, .NET

Automatikgetriebe für den Datenlayer

3. Februar 2006

Heute halte ich den zweiten von mir veröffentlichten Artikel im dotnet magazin in meinen Händen. Das Heft ist ab 8.2.2006 am Kiosk erhältlich!

Im Artikel beschreibe ich die grundsätzliche Vorgehensweise und Verwendung von MyGeneration, einem templatebasierenden Codegenerator. Mit dem enthaltenen OR/Mapper dOOdads lassen sich schnell und einfach .NET Datenbankanwendungen erstellen. Ich habe das Tool schon in einer Reihe von Projekten eingesetzt und bin von der Einfachheit und Effizienz begeistert! Sicherlich gibt es OR/Mapper die deutlich mehr Funktionalitäten enthalten, doch für kleine und einfache Anwendungen ist die Kombination von MyGenertion und dOOdads meiner Ansicht nach optimal.

.NET, Artikel