TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
L A
NA
170
171.3k
Logging to different types - log4net
Jul 9 2018 1:35 AM
Hi, I'm using Log4net in an ASP.Net MVC for logging to different output types (file/ console & DB).
Added new log4net.config to project
<
log4net
>
<
root
>
<
level
value
=
"ALL"
/>
<
appender-ref
ref
=
"file"
/>
<
appender-ref
ref
=
"DBLog"
/>
</
root
>
<
appender
name
=
"file"
type
=
"log4net.Appender.RollingFileAppender"
>
<
file
value
=
"localDirPath\myapp.log"
/>
<
appendToFile
value
=
"true"
/>
<
rollingStyle
value
=
"Size"
/>
<
maxSizeRollBackups
value
=
"5"
/>
<
maximumFileSize
value
=
"5MB"
/>
<
staticLogFileName
value
=
"true"
/>
<
layout
type
=
"log4net.Layout.PatternLayout"
>
<
conversionPattern
value
=
"%-7p%d{yyyy-MM-dd HH:mm:ss tt} –%X{user}– %m method:%method %n stacktrace:%stacktrace{5} %n %logger %n type:%type %n line: %line %n"
/>
</
layout
>
</
appender
>
<
appender
name
=
"DBLog"
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
=
"Server=serverName;Database=DemoDB;integrated security=True;persist security info=True;"
/>
<
commandText
value
="INSERT INTO dbo.Log4net_Log ([Date],[Level],[Logger],[User],[Message],[Exception])
VALUES (@log_date, @log_level, @logger, @user, @message, @exception)"
/>
<
parameter
>
<
parameterName
value
=
"@log_date"
/>
<
dbType
value
=
"DateTime"
/>
<
layout
type
=
"log4net.Layout.RawTimeStampLayout"
/>
</
parameter
>
<
parameter
>
<
parameterName
value
=
"@log_level"
/>
<
dbType
value
=
"String"
/>
<
size
value
=
"50"
/>
<
layout
type
=
"log4net.Layout.PatternLayout"
>
<
conversionPattern
value
=
"%p"
/>
</
layout
>
</
parameter
>
<
parameter
>
<
parameterName
value
=
"@logger"
/>
<
dbType
value
=
"String"
/>
<
size
value
=
"255"
/>
<
layout
type
=
"log4net.Layout.PatternLayout"
>
<
conversionPattern
value
=
"%c"
/>
</
layout
>
</
parameter
>
<
parameter
>
<
parameterName
value
=
"@user"
/>
<
dbType
value
=
"String"
/>
<
size
value
=
"50"
/>
<
layout
type
=
"log4net.Layout.PatternLayout"
>
<
conversionPattern
value
=
"%X{user}"
/>
</
layout
>
</
parameter
>
<
parameter
>
<
parameterName
value
=
"@message"
/>
<
dbType
value
=
"String"
/>
<
size
value
=
"4000"
/>
<
layout
type
=
"log4net.Layout.PatternLayout"
>
<
conversionPattern
value
=
"%m"
/>
</
layout
>
</
parameter
>
<
parameter
>
<
parameterName
value
=
"@exception"
/>
<
dbType
value
=
"String"
/>
<
size
value
=
"2000"
/>
<
layout
type
=
"log4net.Layout.ExceptionLayout"
/>
</
parameter
>
</
appender
>
</
log4net
>
In line # 22 i'm using connection stringof my DB to which logs would be written, but i'm already using same connection string in 'web.config' to fetch data and populate on UI.
How to use connection string specified in web.config for logging data to table using log4net.
As i'm logging different parameters to file such as time, userName, method and stacktrace; All log levels (Info, warn, error) log complete parameters, which i'm not intresered in. How to log only time, userName, method under INFO level & time, userName, method, stacktrace under WARN/ Error level
Added a new log4netHelper.cs to project
public
class
log4netHelper
{
private
static
readonly
ILog log4Net = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public
log4netHelper()
{
if
(HttpContext.Current.User !=
null
&& HttpContext.Current.User.Identity.IsAuthenticated)
MDC.Set(
"user"
, HttpContext.Current.User.Identity.Name);
}
public
static
void
LogMessage(
string
message)
{
log4Net.Info(message);
}
public
static
void
LogError(
string
message, Exception ex)
{
log4Net.Error(message, ex);
}
}
How to get current caller method which is calling log4net.Message()? (MSDN says to use 'System.Runtime.CompilerServices.CallerMemberName' attribute, but how to incorporate this into my logic.
Current log4net.config have both RollingFileAppender & AdoNetAppender but only RollingFileAppender is logging data to file, nothing is logged to Database table.
Reply
Answers (
2
)
Connecting visual studio 2017 to 64-bit excel file
how to make my database layer connect to multiple SQL db