I have an SmtpAppender like this:
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="MyEmail#example.org" />
<from value="SenderEmail#example.org" />
<subject type="log4net.Util.PatternString" value="This is a subject" />
<smtpHost value="smtp.host.value" />
<authentication value="Basic" />
<port value="587" />
<username value="Username" />
<password value="Password" />
<bufferSize value="1" />
<EnableSsl value="true"/>
<lossy value="false" />
<threshold value="ERROR" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%3thread] %-5level [%class].[%method] - %message%newline" />
</layout>
</appender>
I would like to change the subject of the log mail in my code to a string I would specify so it suits my needs better than a standard subject.
Right now I achieve it like this. First, I find the SmtpAppender by name:
public static IAppender FindAppenderByName(string name)
{
ILoggerRepository rootRep = LogManager.GetRepository();
foreach (IAppender iApp in rootRep.GetAppenders()) {
if (string.Compare(name, iApp.Name, true) == 0) {
return iApp;
}
}
return null;
}
and then I can change the subject like this:
IAppender appender = FindAppenderByName("SmtpAppender");
SmtpAppender smtpAppender = (SmtpAppender)appender;
smtpAppender.Subject = "An error occured trying to convert the values";
log.Error("Error message");
But this seems to be a bit too complicated since every time I send an error mail I first have to find the appender by name and I don't want to use a global IAppender object as well as that seems like bad practice to me.
Is there a way to use the user-specified error message as a subject or is there an extension method I could use?
My proposal is as follows :
Create a new class extending SmtpAppender
Override SendBuffer
Based on your LoggingEvent, dynamically change the base.Subject
public class CustomSmtpAppender : SmtpAppender
{
protected override void SendBuffer(LoggingEvent[] events)
{
PrepareSubject(events);
base.SendBuffer(events);
}
protected virtual void PrepareSubject(IEnumerable<LoggingEvent> events)
{
//here you can eval events and set base.Subject
}
}
Related
I have implemented log4net into .NET core 2.0, to log into a text file. As log4net have a config file, which is having XML configuration in it. So, I have created a separate file log4net.config to set its configuration and it is working fine. But I want to set its configuration into appsettings.json. Is it possible to write the log4net configuration into appsettings.json.
<appSettings>
<add key="IsLog" value="True" />
<add key="MaxThreads" value="3" />
</appSettings>
<log4net debug="false">
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./logs/logfile.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
this is my XML configuration.
I have found this question in which it is mentioned that log4net don't support Json projects. Is it possible to write its configuration into appsettings.json.
Maybe it is not the right way but, anyhow I managed to use my log4net.config in appSettings.json. I am putting my answer here so it can help someone if they don't want to use an extra config file for there project.
So what I have done is like by converting my XML into JSON and used the JSON as a string on my appSettings.json after that I use the following code to read the string.
appSettings.json
{
"ConnectionStrings": {
"LoggerConfig": "Config string"
}
}
Json to XML Conversion using Newtonsoft.Json
string xmlElement = _configuration["ConnectionStrings:LoggerConfig"];
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(xmlElement);
XmlConfigurator.Configure(logRepository, GenerateStreamFromString(doc.InnerXml));
This code is used to convert the JSON into XML but it will not provide the XML content as Element so, I used it as a stream.
For converting the string into a stream I used the following code.
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
and it works fine.
Here I used to convert first my XML to JSON and again JSON to XML to
use it as a log4net config, but if anyone wants then use XML
directly as a string, So it will reduce some code.
If you using Microsoft.Extensions.Logging.Log4Net.AspNetCore nuget package, there is a way keep log4net config in appsettings.json (but honestly not very usable).
You can write into appsettings.json (or appsettings.Environment.json for different environments) rules overriding nodes from log4net config file.
Documentation
Example of setting the logging level from appsettings.json.
appsettings.json:
{
"Log4NetCore": {
"PropertyOverrides": [
{
"XPath": "/log4net/root/level",
"Attributes": {
//"value": "ALL"
//"value": "DEBUG"
//"value": "INFO"
"value": "WARN"
//"value": "ERROR"
//"value": "FATAL"
//"value": "OFF"
}
}
]
}
}
You still needs log4net config file with nodes which you want override from appsettings.json:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="example.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DebugAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Registration in Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Add these lines
var loggingOptions = this.Configuration.GetSection("Log4NetCore")
.Get<Log4NetProviderOptions>();
loggerFactory.AddLog4Net(loggingOptions);
app.UseMvc();
}
}
I would like to send an email only after 100 ERRORS were written as a html table(That's the purpose of the SmtpExtendedAppender, iterate over all the messages that were saved) but I am not sure how to access them.
This is my app.config:
<appender name="SmtpAppender" type="log4net.Appender.SmtpExtendedAppender">
<authentication value="Basic" />
<password value="xxxxxx"/>
<username value="xxxxxxxx"/>
<from value="myemail#gmail.com" />
<to value="toemail#gmail.com" />
<smtpHost value="smtp.gmail.com" />
<isBodyHtml value="true" />
<bufferSize value="100" />
<EnableSsl value="true"/>
<subject value="test logging message" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger]%newlineUsername: %property{username}%newline%level - %message%newline%exception" />
</layout>
</appender>
This is my console application:
static void Main(string[] args)
{
Console.WriteLine("hello world");
log.Error("This is my first error message");
log.Error("This is my second error message");
Console.ReadLine();
}
I am not fully understand how the log4net buffer size works, even though I gave it a value of 100, it sends me 2 separated emails every time I run my console application.
If I will run this application 50 times to get to 100 ERRORS, would it be possible to accumulate them and then send them in one email?
No that is not possible. The appender works not over multiple instances of your application. Every time you restart your application, all buffers are empty.
Ok, here's the appender:
<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
<file value="debug.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.PropertyFilter">
<Key value="ApplicationName" />
<StringToMatch value="Test Application" />
</filter>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="Date:%date Thread:[%thread] Level:%-5level Logger:%logger - ApplicationName:%property{ApplicationName}; Message:%message%newline" />
</layout>
</appender>
Now, it seems this would check the ThreadContext.Properties["ApplicationName"] string and if it finds 'Test Application' it would log.
Well, it's logging everything, even if ApplicationName="DoNotLog".
Now, I will freely admit this may be due to the way I'm (attempting) to use Log4Net - I need to expose it to COM, so I've wrapped up a singleton Log4Net instance, and call this from COM:
public void LogDebug(LogCodeSource codeSource, LogExecutionSource execSource, object message, Exception exception = null)
{
// Add the thread context properties for appender filtering purposes
log4net.ThreadContext.Properties["ApplicationName"] = codeSource.ApplicationName;
log4net.ThreadContext.Properties["ComponentName"] = codeSource.ComponentName;
log4net.ThreadContext.Properties["MethodName"] = codeSource.MethodName;
log4net.ThreadContext.Properties["ClientMachineName"] = execSource.ClientMachineName;
log4net.ThreadContext.Properties["ServerMachineName"] = execSource.ServerMachineName;
log4net.ThreadContext.Properties["UserName"] = execSource.UserName;
if (exception == null)
{
LoggerContext.GetInstance.MainLogger.Debug(message);
}
else
{
LoggerContext.GetInstance.MainLogger.Debug(message, exception);
}
}
In my tests, if I call:
var logger = new MyLogger();
logger.LogDebug(new LogCodeSource { ApplicationName = "Test Application", ComponentName = "Test component", MethodName = "Test method" }, new LogExecutionSource { ClientMachineName = "Test client", ServerMachineName = "Test server", UserName = "Test user" }, "Test message");
logger.LogDebug(new LogCodeSource { ApplicationName = "DoNotLog", ComponentName = "Test component", MethodName = "Test method" }, new LogExecutionSource { ClientMachineName = "Test client", ServerMachineName = "Test server", UserName = "Test user" }, "Test message");
In root, I've tried:
<root>
<appender-ref ref="DebugFileAppender" />
</root>
As well as:
<logger name="MyLogger">
<level value="DEBUG" />
<appender-ref ref="DebugFileAppender" />
</logger>
Both logs are showing up in my debug.log file.
Any idea why the property filter isn't preventing DoNotLog from going to debug.log?
Here's what I'm seeing in the log:
Date:2015-12-31 11:11:00,928 Thread:[14] Level:DEBUG Logger:MyLogger - ApplicationName:Test Application; Message:Test message
Date:2015-12-31 11:11:00,942 Thread:[14] Level:DEBUG Logger:MyLogger - ApplicationName:DoNotLog; Message:Test message
What you are after is an AND filter since you want to log all message where ApplicationName matches a particular name AND the level is DEBUG. Unfortunately log4net doesn't ship AND filters out of the box however this SO answer details how to implement one.
Property filter are also available when using Microsoft.Extensions.Logging.Abstraction instead if log4Net directly.
Example:
using (_logger.BeginScope(new[] { new KeyValuePair<string, object>("YourPropertyKey", "YourPropertyValue") }))
{
_logger.LogDebug("My special log with context property set");
}
Then in the config an appender has to be setup for the property and key.
Note there is rexexToMatch in oder to stringToMacth if you want to allow multiple values to a key.
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="property_logger" />
</root>
<appender name="property_logger" type="log4net.Appender.RollingFileAppender">
<file value="logs/my_service.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="-yyyyMMdd" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<preserveLogFileNameExtension value="true" />
<staticLogFileName value="false" />
<filter type="log4net.Filter.PropertyFilter">
<Key value="YourPropertyKey" />
<StringToMatch value="YourPropertyValue" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%2thread] %-5level %.50logger - %message%newline" />
</layout>
</appender>
</log4net>
i just want to know that, what is use of log4net in application and how i`m gonna implement in c#.
i am done with some of the research that i am put under it.
public static class Logger
{
private static log4net.ILog Log { get; set; }
static Logger()
{
Log = log4net.LogManager.GetLogger(typeof(Logger));
}
public static void Error(object msg)
{
Log.Error(msg);
}
public static void Error(object msg, Exception ex)
{
Log.Error(msg, ex);
}
public static void Error(Exception ex)
{
Log.Error(ex.Message, ex);
}
public static void Info(object msg)
{
Log.Info(msg);
}
}
}
above is just a method that maintain log in separate file i just want to know in advance.
1.First you have to add a reference for log4net.dll if you havn't done that yet.
2.Then you have to add that statement above your stating class declaration. You can put it right bellow your using statements and outside of your class declaration:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
3.The class that you already have created.
4.Add that config to your configSections tag on your web.config/app.config:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
That will allow you to add tags to configure your log.
5.Add that to your configuration tag on your web.config/app.config:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./Log/log.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="'.'yyyy_MM_dd" />
<maxSizeRollBackups value="30" />
<maximumFileSize value="100MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date{yyyy/mm/dd HH:mm:ss,fff} %-5level %logger [%thread] %message %exception%newline"/>
</layout>
</appender>
</log4net>
6.Then you just have to call the methods on your Logger class.
If you want more information i'll sugest you to read the blog of that guy Log4Net Tutorials and Resources
Does anyone know how to specify the date format so that leading zeros do not appear in the log? I know it sounds very picky, but another process is reading my logs and seems not to like leading zeros.
I am using:
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="NamespaceName.ClassName" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<param name="RemoteAddress" value="192.168.1.123" />
<param name="RemotePort" value="514" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{MM/dd/yyyy hh:mm:ss tt} %message %newline" />
</layout>
</appender>
And I get this:
07/10/2012 09:20:39 AM ...
If possible, I'd like to get:
7/10/2012 9:20:39 AM
I tried simply using: %date{M/dd/yyyy h:mm:ss tt}
But it didn't work (and also seems odd for when two chars are needed)
Edit on 07/15/12:
I am not yet back at the office so I haven't checked on the UDP bug yet, but in case anyone else out there wants to extend an appender simply to add a property to pass into your app:
public class SyslogAppender : UdpAppender
{
public string UpdateIp { get; set; }
// ReSharper disable RedundantOverridenMember
override protected void Append(LoggingEvent loggingEvent)
{
base.Append(loggingEvent);
}
// ReSharper restore RedundantOverridenMember
}
<appender name="UdpAppender" type="Namespace.Log4Net.SyslogAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Namespace.LogGenFileReparser" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="Namespace.Log4Net.SyslogAppender" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<param name="RemoteAddress" value="192.168.73.133" />
<param name="RemotePort" value="514" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{M/dd/yyyy h:mm:ss tt} %message %newline" />
</layout>
<param name="UpdateIp" value="false" />
</appender>
I check the new param before calling configure:
var log4FileConfigFile = XDocument.Parse(File.ReadAllText("log4net.config"));
var firstOrDefault = log4FileConfigFile.Descendants("log4net").Descendants("appender").
Select(appender => appender.Attributes().
FirstOrDefault(attrib => attrib.Name == "name" && attrib.Value == "UdpAppender")).
FirstOrDefault(check => null != check);
if (firstOrDefault != null)
if (firstOrDefault.Document != null)
{
var xAttribute2 = firstOrDefault.Document.Descendants("param").
Attributes().FirstOrDefault(attrib => attrib.Value == "UpdateIp");
if (xAttribute2 != null && xAttribute2.Value.ToLower().Equals("true"))
{
var xAttribute = firstOrDefault.Document.Descendants("param").
Attributes().FirstOrDefault(attrib => attrib.Value == "RemoteAddress");
if (xAttribute != null)
xAttribute.NextAttribute.SetValue(
IPAddresses.GetLocalIp().ToString());
}
}
XmlConfigurator.Configure(...
Edit 7/17/12
I had no other log4net error (with UDP appender). The problem was in the receiver.
Works for me but I'm logging to a file.
Are you sure whatever you are using the view the log isn't re-formatting the output?