Il Log è una parte importante di molti programmi.
Esiste un Framework molto valido, dal lontano 1996 su linguaggio Java, che permette in sintesi:
- Logging su più canali
- Gerarchie di log
- Filtri
- Configurabile da file (con watcher)
Il sito ufficiale è: http://logging.apache.org/ log4net/
Ecco un esempio di log4net.config file:
<!-- This section contains the log4net configuration settings --> <log4net> <!-- File Appender --> <appender name="LogFileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.txt" /> <sppendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <header value="[Header]
" /> <footer value="[Footer]
" /> <conversionPattern value="%date [%thread] %-5level %logger (%property{log4net:HostName}) [%ndc] - %message%newline" /> </layout> </appender> <!-- Rolling File! :) --> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="example.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level %thread %logger - %message%newline" /> </layout> </appender> <!-- Console Appender --> <!-- appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender --> <!-- EventID Appender --> <!-- Also note that if your program is called app.exe, then you need an eventlog source called app.exe. If this does not exist, then Log4net will attempt to create it, but that requires Admin rights, so you may need to run your program as admin at least once in order to create this event source. To avoid this, the event source would normally be created as part of the installation procedure which would already be running as admin. --> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender"> <!--applicationName value="log4net_lab" /--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!-- Database Appender --> <!-- Example of how to configure the AdoNetAppender to connect to MS SQL Server --> <appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=your_host catalog=your_database;integrated security=false;persist security info=True;User ID=your_user;Password=your_password" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" /> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <!-- <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" /> </layout> --> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="50" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!-- SQLite Appender --> <appender name="AdoNetAppender_SQLite" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.96.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" /> <connectionString value="Data Source=log4net.db3;Version=3;" /> <commandText value="INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" /> <parameter> <parameterName value="@Date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@Level" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@Logger" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@Message" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!-- Assign Appenders --> <root> <level value="DEBUG" /> <!--level value="All"/--> <appender-ref ref="LogFileAppender" /> <appender-ref ref="RollingFileAppender" /> <!--appender-ref ref="ConsoleAppender"/--> <appender-ref ref="EventLogAppender" /> <!--appender-ref ref="ADONetAppender_SqlServer"/--> <appender-ref ref="AdoNetAppender_SQLite" /> </root> <!-- Print only messages of level WARN or above in the package Com.Foo --> <logger name="Com.Foo"> <level value="WARN" /> </logger> </log4net>