using log4net to log to databse

sachinnair1

New Member
Hi,
I am trying to use log4net to log every thing to a mssql 2008 database. Here is what i have

DatabaseLogging.ps1 file:
Code:
$ErrorActionPreference="Stop"
[void][Reflection.Assembly]::LoadFile("C:\Log4net\log4net.dll")
 
[log4net.LogManager]::ResetConfiguration()
 
$configFileInfo = gi "C:\Log4net\log4net.xml"
 
[log4net.GlobalContext]::Properties["timestamp"] = Get-Date -Format "yyyy-MMM-dd_hh-mm-ss"
 
[log4net.Config.XmlConfigurator]::ConfigureAndWatch($configFileInfo)
 
$logger = [log4net.LogManager]::GetLogger("Test")
 
$logger.info("Build=$latestFullBuild")
 
$logger = [log4net.LogManager]::GetLogger("PowerShell")
 
try
{
$logger.Info("Started the main script...$i");
$logger.Debug("This is the debugging information");
$logger.Warn("The warning is that if you try to create a file with the same name it will fail");
 
New-Item -ItemType file freespace.txt
New-Item -ItemType file freespace.txt 
}
catch
{ 
$logger.Error("I told ya! You have an error");
$logger.Fatal($_.Exception.message);
$logger.Fatal($_.Exception.Message,$_.Exception);
}
finally
{
$logger.Info("Exiting Test-Excp() method")
}
Log4net.xml File
<log4net>
<appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=.;initial catalog=DBLog;integrated security=false;persist security info=True;User ID=sa;Password=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" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
- <parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%thread" />
</parameter>
- <parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
- <parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
- <parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</parameter>
</appender>
 
<root>
<level value="All" />
<appender-ref ref="AdoNetAppender_SqlServer" /> 
</root>
<logger name="Test" additivity="true">
</logger>
</log4net>
So now when i execute the ps1 file it shows no error but when i check the database there is nothing in it. I really dont understand what went wrong but nothing is logged to the database. I tried the "log4net.Appender.FileAppender " it works fine, logs eveything.
Can someone please help me out with this ?
Thanks in advance.
 

My Computer

Top