Simple way to perform error logging? - c#

I've created a small C# winforms application, as an added feature I was considering adding some form of error logging into it. Anyone have any suggestions for good ways to go about this? This is a feature I've never looked into adding to previous projects, so I'm open to suggestions from Developers who have more experience.
I was considering something along the lines of writing exceptions to a specified text file, or possibly a database table. This is an application that will be in use for a few months and then discarded when a larger product is finished.

I wouldn't dig too much on external libraries since your logging needs are simple.
.NET Framework already ships with this feature in the namespace System.Diagnostics, you could write all the logging you need there by simply calling methods under the Trace class:
Trace.TraceInformation("Your Information");
Trace.TraceError("Your Error");
Trace.TraceWarning("Your Warning");
And then configure all the trace listeners that fit your needs on your app.config file:
<configuration>
// other config
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
<add name="textWriterListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="YourLogFile.txt"/>
<add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="YourEventLogSource" />
<remove name="Default"/>
</listeners>
</trace>
</system.diagnostics>
// other config
</configuration>
or if you prefer, you can also configure your listeners in your application, without depending on a config file:
Trace.Listeners.Add(new TextWriterTraceListener("MyTextFile.log"));
Remember to set the Trace.AutoFlush property to true, for the Text log to work properly.

You could use SimpleLog.
It's a simple, but robust and powerful one-class logging solution, easy to understand, easy to integrate and easy to use. No need to spend days for setting up and customize log4Net, with that class, you're done in minutes.
Though it currently logs to a file, it should be easily customizable to log to a database.
http://www.codeproject.com/Tips/585796/Simple-Log

An optimal solution, in my opinion, would be to use NLog: http://nlog-project.org/
Just install the config package from NuGet: http://www.nuget.org/packages/NLog.Config/ and you will end up with the library and a pre-configured file logger...
Then in your code you just need:
// A logger member field:
private readonly Logger logger = LogManager.GetCurrentClassLogger(); // creates a logger using the class name
// use it:
logger.Info(...);
logger.Error(...);
// and also:
logger.ErrorException("text", ex); // which will log the stack trace.
In the config file you get, you need to uncomment the sections that you need:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- add your targets here -->
<!-- UNCOMMENT THIS!
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
</targets>
<rules>
<!-- add your logging rules here -->
<!-- UNCOMMENT THIS!
<logger name="*" minlevel="Trace" writeTo="f" />
-->
</rules>
</nlog>
Edit the properties of the nlog.config file to
Copy to Output Directory: Copy always

Create a class called Log.cs
I am using Linq To SQl to save to the database
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
public static partial class Log
{
/// <summary>
/// Saves the exception details to ErrorLogging db with Low Priority
/// </summary>
/// <param name="ex">The exception.</param>
public static void Save(this Exception ex)
{
Save(ex, ImpactLevel.Low, "");
}
/// <summary>
/// Saves the exception details to ErrorLogging db with specified ImpactLevel
/// </summary>
/// <param name="ex">The exception.</param>
/// <param name="impactLevel">The Impact level.</param>
public static void Save(this Exception ex, ImpactLevel impactLevel)
{
Save(ex, impactLevel,"");
}
/// <summary>
/// Saves the exception details to ErrorLogging db with specified ImpactLevel and user message
/// </summary>
/// <param name="ex">The exception</param>
/// <param name="impactLevel">The impact level.</param>
/// <param name="errorDescription">The error Description.</param>
public static void Save(this Exception ex, ImpactLevel impactLevel, string errorDescription)
{
using (var db = new ErrorLoggingDataContext())
{
Log log = new Log();
if (errorDescription != null && errorDescription != "")
{
log.ErrorShortDescription = errorDescription;
}
log.ExceptionType = ex.GetType().FullName;
var stackTrace = new StackTrace(ex, true);
var allFrames = stackTrace.GetFrames().ToList();
foreach (var frame in allFrames)
{
log.FileName = frame.GetFileName();
log.LineNumber = frame.GetFileLineNumber();
var method = frame.GetMethod();
log.MethodName = method.Name;
log.ClassName = frame.GetMethod().DeclaringType.ToString();
}
log.ImpactLevel = impactLevel.ToString();
try
{
log.ApplicationName = Assembly.GetCallingAssembly().GetName().Name;
}
catch
{
log.ApplicationName = "";
}
log.ErrorMessage = ex.Message;
log.StackTrace = ex.StackTrace;
if (ex.InnerException != null)
{
log.InnerException = ex.InnerException.ToString();
log.InnerExceptionMessage = ex.InnerException.Message;
}
log.IpAddress = ""; //get the ip address
if (System.Diagnostics.Debugger.IsAttached)
{
log.IsProduction = false;
}
try
{
db.Logs.InsertOnSubmit(log);
db.SubmitChanges();
}
catch (Exception eex)
{
}
}
}
}
Create the following table
USE [database Name]
GO
/****** Object: Table [dbo].[Log] Script Date: 9/27/2016 11:52:32 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Log](
[LogId] [INT] IDENTITY(1,1) NOT NULL,
[ErrorDate] [DATETIME] NOT NULL CONSTRAINT [DF_Log_Date] DEFAULT (GETDATE()),
[ErrorShortDescription] [VARCHAR](1000) NULL,
[ExceptionType] [VARCHAR](255) NULL,
[FileName] [VARCHAR](1000) NULL,
[LineNumber] [INT] NULL,
[MethodName] [VARCHAR](255) NULL,
[ClassName] [VARCHAR](150) NULL,
[ImpactLevel] [VARCHAR](50) NOT NULL,
[ApplicationName] [VARCHAR](255) NULL,
[ErrorMessage] [VARCHAR](4000) NULL,
[StackTrace] [VARCHAR](MAX) NULL,
[InnerException] [VARCHAR](2000) NULL,
[InnerExceptionMessage] [VARCHAR](2000) NULL,
[IpAddress] [VARCHAR](150) NULL,
[IsProduction] [BIT] NOT NULL CONSTRAINT [DF_Log_IsProduction] DEFAULT ((1)),
[LastModified] [DATETIME] NOT NULL CONSTRAINT [DF_Log_LastModified] DEFAULT (GETDATE()),
CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED
(
[LogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty #name=N'MS_Description', #value=N'This table holds all the exceptions.
ErrorData = when error happened
,[ErrorShortDescription] == short desc about the error entered by the developers
,[FileName] = file where error happened full path
,[LineNumber] = line number where code failed
,[MethodName] = method name where exception happened
,[ClassName] = class where exception happened
,[ImpactLevel] = high, medium, low
,[ApplicationName] = name of the application where error came from
,[ErrorMessage] = exception error messge
,[StackTrace] = C# stack trace
,[InnerException] = inner exception of strack trace
,[InnerExceptionMessage] = inner message
,[IpAddress]
,[IsProduction]' , #level0type=N'SCHEMA',#level0name=N'dbo', #level1type=N'TABLE',#level1name=N'Log'
GO
Impact Level is basically Enum
public enum ImpactLevel
{
High = 0,
Medium = 1,
Low = 2,
}
You can use it as following
try
{
}
catch(Exception ex)
{
//this will save the exception details and mark exception as low priority
ex.Save();
}
try
{
}
catch(Exception ex)
{
//this will save the exception details with priority you define: High, Medium,Low
ex.Save(ImpactLevel.Medium);
}
try
{
}
catch(Exception ex)
{
//this will save the exception details with priority you define: High, Medium,Low
ex.Save(ImpactLevel.Medium, "You can enter an details you want here ");
}

Well log4net works like a brick. It may be a bit hard to configure, but its worth it. It also allows you to configure file locking of those log files etc.
http://www.codeproject.com/Articles/140911/log4net-Tutorial

After reading the suggestions here, I ended up using the following:
private void LogSystemError(string message)
{
EventLog.WriteEntry("YourAppName", message, EventLogEntryType.Error);
}
The EventLog class is available using System.Diagnostics.
I avoided the options of logging into files (e.g. "yourLogFile.txt") to avoid issues of concurrency of multiple threads logging errors, location of the file and access security, and the possible issues of having a file that grows too large.

Heres example for log4net:
Create a new console project called Log4NetTest
Add log4net [1.2.13] nuget package into project
Write following program:
using System.Threading.Tasks;
using log4net;
using System.Text;
using System.CollectionsGeneric;
using System;
namespace Log4NetTest
{
class Program
{
private static readonly ILog _logger = LogManager.GetLogger("testApp.LoggingExample");
static void Main(string[] args)
{
// Configure from App.config. This is marked as obsolete so you can also add config into separate config file
// and use log4net.Config.XmlConfigurator method to configure from xml file.
log4net.Config.DOMConfigurator.Configure();
_logger.Debug("Shows only at debug");
_logger.Warn("Shows only at warn");
_logger.Error("Shows only at error");
Console.ReadKey();
}
}
}
Change your app.config to following:
<!-- language: xml -->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="myLog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<category name="testApp.LoggingExample">
<priority value="ALL" />
</category>
</log4net>
</configuration>
5.Run application and you should find following file from bin\Debug folder:
2013-12-13 13:27:27,252 [8] DEBUG testApp.LoggingExample (null) - Shows only at debug
2013-12-13 13:27:27,280 [8] WARN testApp.LoggingExample (null) - Shows only at warn
2013-12-13 13:27:27,282 [8] ERROR testApp.LoggingExample (null) - Shows only at error

You just write out your exception errors to a text file. Write to Text File. One suggestion is to put the file you create in a userdata or appdata directory though, so you do not have to struggle with permissions.
Since this is only needed for a few months and will be discarded there is no reason to go overboard with DB. A simple text file should suffice.

Instead of using log4net which is an external library I have created my own simple class, highly customizable and easy to use (edit YOURNAMESPACEHERE with the namespace that you need).
CONSOLE APP
using System;
using System.IO;
namespace YOURNAMESPACEHERE
{
enum LogEvent
{
Info = 0,
Success = 1,
Warning = 2,
Error = 3
}
internal static class Log
{
private static readonly string LogSession = DateTime.Now.ToLocalTime().ToString("ddMMyyyy_HHmmss");
private static readonly string LogPath = AppDomain.CurrentDomain.BaseDirectory + "logs";
internal static void Write(LogEvent Level, string Message, bool ShowConsole = true, bool WritelogFile = true)
{
string Event = string.Empty;
ConsoleColor ColorEvent = Console.ForegroundColor;
switch (Level)
{
case LogEvent.Info:
Event = "INFO";
ColorEvent = ConsoleColor.White;
break;
case LogEvent.Success:
Event = "SUCCESS";
ColorEvent = ConsoleColor.Green;
break;
case LogEvent.Warning:
Event = "WARNING";
ColorEvent = ConsoleColor.Yellow;
break;
case LogEvent.Error:
Event = "ERROR";
ColorEvent = ConsoleColor.Red;
break;
}
if (ShowConsole)
{
Console.ForegroundColor = ColorEvent;
Console.WriteLine(" [{0}] => {1}", DateTime.Now.ToString("HH:mm:ss"), Message);
Console.ResetColor();
}
if (WritelogFile)
{
if (!Directory.Exists(LogPath))
Directory.CreateDirectory(LogPath);
File.AppendAllText(LogPath + #"\" + LogSession + ".log", string.Format("[{0}] => {1}: {2}\n", DateTime.Now.ToString("HH:mm:ss"), Event, Message));
}
}
}
}
NO CONSOLE APP (ONLY LOG)
using System;
using System.IO;
namespace YOURNAMESPACEHERE
{
enum LogEvent
{
Info = 0,
Success = 1,
Warning = 2,
Error = 3
}
internal static class Log
{
private static readonly string LogSession = DateTime.Now.ToLocalTime().ToString("ddMMyyyy_HHmmss");
private static readonly string LogPath = AppDomain.CurrentDomain.BaseDirectory + "logs";
internal static void Write(LogEvent Level, string Message)
{
string Event = string.Empty;
switch (Level)
{
case LogEvent.Info:
Event = "INFO";
break;
case LogEvent.Success:
Event = "SUCCESS";
break;
case LogEvent.Warning:
Event = "WARNING";
break;
case LogEvent.Error:
Event = "ERROR";
break;
}
if (!Directory.Exists(LogPath))
Directory.CreateDirectory(LogPath);
File.AppendAllText(LogPath + #"\" + LogSession + ".log", string.Format("[{0}] => {1}: {2}\n", DateTime.Now.ToString("HH:mm:ss"), Event, Message));
}
}
Usage:
CONSOLE APP
Log.Write(LogEvent.Info, "Test message"); // It will print an info in your console, also will save a copy of this print in a .log file.
Log.Write(LogEvent.Warning, "Test message", false); // It will save the print as warning only in your .log file.
Log.Write(LogEvent.Error, "Test message", true, false); // It will print an error only in your console.
NO CONSOLE APP (ONLY LOG)
Log.Write(LogEvent.Info, "Test message"); // It will print an info in your .log file.

Related

How to configure log4net with VSTO project in visual studio

I was trying to build a simple outlook add in.
I created a VSTO project in Visual Studio 2017.
When the project is created by the Visual Studio, there is no app.config or web.config in the solution. I want to use log4net for this project. How should I configure it? I tried to add web.config or app.config for the project. But I was not able to get values from the configure file. I think the project cannot recognize them. I cannot use ConfigurationManager.AppSettings["key"] to get the value from the configure file.
Does anyone know how to use log4net in VSTO project?
Thank you.
Install Log4Net through the NuGet Package Manager. Then create a setting in the project properties, like asdf (you can delete the setting after you update the app.config file with the Log4Net sections), it will then create the app.config for you.
Here's the configuration in app.config for Log4Net I use on most of my projects. I created a new project with a setting asdf and added my standard Log4Net setup.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ExcelAddIn1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<userSettings>
<ExcelAddIn1.Properties.Settings>
<setting name="asdf" serializeAs="String">
<value>asdf</value>
</setting>
</ExcelAddIn1.Properties.Settings>
</userSettings>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/>
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\Temp\MyOfficeAddIn.log"/>
<appendToFile value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date|%-5level|%message%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
I usually create a class called ErrorHandler and add the following code.
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using log4net;
using log4net.Config;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Then I use the following methods to write to the log file
private static readonly ILog log = LogManager.GetLogger(typeof(ErrorHandler));
/// <summary>
/// Applies a new path for the log file by FileAppender name
/// </summary>
public static void SetLogPath()
{
XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
string logFileName = System.IO.Path.Combine(Properties.Settings.Default.App_PathLocalData, AssemblyInfo.Product + ".log");
foreach (var a in h.Root.Appenders)
{
if (a is log4net.Appender.FileAppender)
{
if (a.Name.Equals("FileAppender"))
{
log4net.Appender.FileAppender fa = (log4net.Appender.FileAppender)a;
fa.File = logFileName;
fa.ActivateOptions();
}
}
}
}
/// <summary>
/// Create a log record to track which methods are being used.
/// </summary>
public static void CreateLogRecord()
{
try
{
// gather context
var sf = new System.Diagnostics.StackFrame(1);
var caller = sf.GetMethod();
var currentProcedure = caller.Name.Trim();
// handle log record
var logMessage = string.Concat(new Dictionary<string, string>
{
["PROCEDURE"] = currentProcedure,
["USER NAME"] = Environment.UserName,
["MACHINE NAME"] = Environment.MachineName
}.Select(x => $"[{x.Key}]=|{x.Value}|"));
log.Info(logMessage);
}
catch (Exception ex)
{
ErrorHandler.DisplayMessage(ex);
}
}
/// <summary>
/// Used to produce an error message and create a log record
/// <example>
/// <code lang="C#">
/// ErrorHandler.DisplayMessage(ex);
/// </code>
/// </example>
/// </summary>
/// <param name="ex">Represents errors that occur during application execution.</param>
/// <param name="isSilent">Used to show a message to the user and log an error record or just log a record.</param>
/// <remarks></remarks>
public static void DisplayMessage(Exception ex, Boolean isSilent = false)
{
// gather context
var sf = new System.Diagnostics.StackFrame(1);
var caller = sf.GetMethod();
var errorDescription = ex.ToString().Replace("\r\n", " "); // the carriage returns were messing up my log file
var currentProcedure = caller.Name.Trim();
var currentFileName = AssemblyInfo.GetCurrentFileName();
// handle log record
var logMessage = string.Concat(new Dictionary<string, string>
{
["PROCEDURE"] = currentProcedure,
["USER NAME"] = Environment.UserName,
["MACHINE NAME"] = Environment.MachineName,
["FILE NAME"] = currentFileName,
["DESCRIPTION"] = errorDescription,
}.Select(x => $"[{x.Key}]=|{x.Value}|"));
log.Error(logMessage);
// format message
var userMessage = new StringBuilder()
.AppendLine("Contact your system administrator. A record has been created in the log file.")
.AppendLine("Procedure: " + currentProcedure)
.AppendLine("Description: " + errorDescription)
.ToString();
// handle message
if (isSilent == false)
{
MessageBox.Show(userMessage, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I have a project in GitHub you can use as an example.

Encrypt custom section in app.config

I want to encrypt the following section in my c# app.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ConX" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<ConX>
<add key="SqlSrv" value="0.0.0.0"/>
</ConX>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
...
The following Code doesn't work, because the NameValueCollection does not support the ProtectSection Method.
How can I encrypt this section?
var section = ConfigurationManager.GetSection("ConX") as NameValueCollection;
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
When I try the following line, the section object just stays null
AppSettingsSection section = ConfigurationManager.GetSection("ConX") as AppSettingsSection;
However, if i read the value like shown below it works, so the section can be found.
var section = ConfigurationManager.GetSection("ConX") as NameValueCollection;
var value = section["SqlSrv"];
I've just implemented the same thing for my website this week, you may find the following code useful.
Kind regards.
/// <summary>
/// Encrypts one or more sections of the web.config using the provided provider.
/// </summary>
/// <param name="protectionProvider">
/// Protection provider to use:
/// RsaProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider.
/// </param>
/// <param name="sectionsToEncrypt">Array of section names to encrypt</param>
/// <returns>
/// On success returns true
/// On failure returns false
/// </returns>
public static bool EncryptConfigurationSections(
string protectionProvider,
params string[] sectionsToEncrypt
) {
bool isOK = true;
List<string> SUPPORTED_PROVIDERS = new List<string>() {
"RsaProtectedConfigurationProvider",
"DPAPIProtectedConfigurationProvider"
};
if (!SUPPORTED_PROVIDERS.Contains(protectionProvider)) {
throw new ArgumentException("Provided provider is not supported.", "protectionProvider");
}
try {
Configuration webConfiguration = null;
bool saveRequired = false;
// OpenWebConfiguration call will find the web.config file, we only need the directory (~)
webConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
// Protect all specified sections
// ... Do all that apply in one go so we only have the hit of saving once
foreach (string sectionToEncrypt in sectionsToEncrypt) {
ConfigurationSection configSection = webConfiguration.GetSection(sectionToEncrypt);
// No point encrypting if it's already been done
if (configSection != null && !configSection.SectionInformation.IsProtected) {
saveRequired = true;
configSection.SectionInformation.ProtectSection(protectionProvider);
configSection.SectionInformation.ForceSave = true;
}
}
if (saveRequired) {
// Only save if there's a section which was not protected
// ... again, no point taking the hit if we don't need to
webConfiguration.Save(ConfigurationSaveMode.Modified);
}
}
catch (Exception e) {
isOK = false;
}
return isOK;
} // EncryptConfigurationSections

Can you configure log4net in code instead of using a config file?

I understand why log4net uses app.config files for setting up logging - so you can easily change how information is logged without needing to recompile your code. But in my case I do not want to pack a app.config file with my executable. And I have no desire to modify my logging setup.
Is there a way for me to set up logging in code rather than using the app.config?
Here is my simple config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\EventLog.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
</appender>
<root>
<level value="Info" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="MemoryAppender" />
</root>
</log4net>
</configuration>
EDIT:
To be completely clear: It is my goal to have no XML file. Not even as an embedded resource that I turn into a stream. My goal was to define the logger completely programmatically. Just curious if it's possible and if so where I might find an example of the syntax.
FINAL SOLUTION:1
For anyone who may stumble upon this in the future, here is what I did. I made the static class below:
using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
namespace Spectrum.Logging
{
public class Logger
{
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = #"Logs\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
}
}
And then all I had to do was replace the code where I called the XML file with the following call:
//XmlConfigurator.Configure(new FileInfo("app.config")); // Not needed anymore
Logger.Setup();
1(this answer was edited into the question by the OP, I took the liberty to make it a community answer, see here why)
You can also escape XML completely, I wrote a sample with minimal programmatic configuration here.
In a nutshell, here is what you need
var tracer = new TraceAppender();
var hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.AddAppender(tracer);
var patternLayout = new PatternLayout {ConversionPattern = "%m%n"};
patternLayout.ActivateOptions();
tracer.Layout = patternLayout;
hierarchy.Configured = true;
Yes, you can configure log4net by calling:
log4net.Config.XmlConfigurator.Configure(XmlElement element)
See the log4net documentation.
Alternatively you could create a custom attribute that inherits from log4net.Config.ConfiguratorAttribute and hard-code you configuration there:
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository;
using log4net.Repository.Hierarchy;
using System;
using System.Reflection;
namespace ConsoleApplication1
{
[AttributeUsage(AttributeTargets.Assembly)]
public class MyConfiguratorAttribute : ConfiguratorAttribute
{
public MyConfiguratorAttribute()
: base(0)
{
}
public override void Configure(Assembly sourceAssembly, ILoggerRepository targetRepository)
{
var hierarchy = (Hierarchy)targetRepository;
var patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
var roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = #"Logs\EventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
}
}
Then add the following to a .cs file:
[assembly: ConsoleApplication1.MyConfigurator]
For those who don't want to add appender to Root logger, but to current/other logger:
//somewhere you've made a logger
var logger = LogManager.GetLogger("MyLogger");
// now add appender to it
var appender = BuildMyAppender();
((log4net.Repository.Hierarchy.Logger)logger).AddAppender(appender);
logger.Debug("MyLogger with MyAppender must work now");
// and remove it later if this code executed multiple times (loggers are cached, so you'll get logger with your appender attached next time "MyLogger")
((log4net.Repository.Hierarchy.Logger)logger).RemoveAppender(sbAppender);
Although the accepted answer works in most cases, It has a few drawbacks.
It only keeps 5 last logs.
Log size is set to 1GB which is too large for most notepad applications to open.
Since it locks the log file, it is not suitable for multi-threaded apps such as web applications.
Since date is prefixed to the file name it is not convenient to use in windows
It overwrites the log each time application re-starts which is again not suitable if you are planning to keep the logs.
As mentioned in comments, it needs some modifications to make it work correctly in some cases.
Thus the little more extensive configuration. I created a class which makes logging a little easier. Obviously you can just pick the configuration part if you wish.
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Web;
public enum LogType { Info, Debug, Warn, Error, Fatal };
public class Logger {
private const string LOG_NAMESPACE = "APP_NAME";
private const string LOG_FILENAME_PREFIX = #"D:\Logs\";
private static readonly Level LOG_LEVEL = Level.Debug;
private static readonly ILog log = LogManager.GetLogger(LOG_NAMESPACE);
#region static Logger()
// Static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced
static Logger() {
Hierarchy hierarchy = (Hierarchy) LogManager.GetRepository();
hierarchy.Root.RemoveAllAppenders(); // Clear all previously added repositories.
hierarchy.Root.Level = LOG_LEVEL; // Set Log level
PatternLayout layout = new PatternLayout() { ConversionPattern = "%d{yyyy-MM-dd HH:mm:ss.fff} %4t %-5p %m%n" }; // Log line format: Include millisecond precision, thread ID, Log type,
layout.ActivateOptions(); // Apply Configuration
RollingFileAppender RFA = new RollingFileAppender();
RFA.Name = LOG_NAMESPACE; // Set name of appender
RFA.File = LOG_FILENAME_PREFIX + LOG_NAMESPACE; // Set file name prefix
RFA.LockingModel = new FileAppender.MinimalLock(); // Minimum lock time required, makes file available for reading
RFA.AppendToFile = true; // Do not overwrite existing logs, append to them.
RFA.DatePattern = ".yyyy.MM.dd'.log'"; // Add file extension here, to preserve the file extension
RFA.Encoding = Encoding.UTF8; // Set format of file to UTF8 for international characters.
RFA.CountDirection = 1; // Increment file name in bigger number is newest, instead of default backward.
RFA.MaximumFileSize = "100MB"; // Maximum size of file that I could open with common notepad applications
RFA.RollingStyle = RollingFileAppender.RollingMode.Composite; // Increment file names by both size and date.
RFA.StaticLogFileName = false;
RFA.MaxSizeRollBackups = -1; // Keep all log files, do not automatically delete any
RFA.PreserveLogFileNameExtension = true; // This plus extension added to DatePattern, causes to rolling size also work correctly
RFA.Layout = layout;
RFA.ActivateOptions(); // Apply Configuration
hierarchy.Root.AddAppender(RFA);
BasicConfigurator.Configure(hierarchy); // Apply Configuration
}
#endregion
#region public static int Log(...)
public static void Log(string Description, LogType logtype = LogType.Info) {
switch (logtype) {
case LogType.Debug:
log.Debug(Description);
break;
case LogType.Info:
log.Info(Description);
break;
case LogType.Warn:
log.Warn(Description);
break;
case LogType.Error:
log.Error(Description);
break;
case LogType.Fatal:
log.Fatal(Description);
break;
}
}
#endregion
#region public static int Log(...)
public static void Log(string Message, Exception ex) {
log.Fatal(Message, ex);
}
#endregion
}
And then to log messages and exceptions call it like following
Logger.Log("I was here", LogType.Debug);
Logger.Log("I am info message");
Logger.Log("An error", LogType.Error);
Logger.Log("An Exception", ex); // ex is of type Exception
Due to the weird way log4net adds rolling data to file name (added after file extension) files loose windows explorer association. To fix that .log was added to DatePattern instead. It will also correctly adds file increment before extension (maybe because of a bug) Tested on version 1.2.11.0
Notes:
No external call for initialization is required, this will initialize on application start (or when you call Logger.Log for the first time
You can move all class constants out to your own config file and make this class more generic and reusable.
Note that no namespace is specified, this will make Logger class available in all namespaces.
Log4net provides 5 methods to log messages which is a little inconvenient to remember. Thus Logger.Log method by default uses info.
An Unrelated note: If your application is running on a server or web, keep your log files off the OS drive and application folder.
The accepted answer works after I found two caveats:
It was not working for me at first, but after using a full absolue path for the roller.File property, it started work.
I had to use this in F# (in a fsx script), so had some issues when converting it from C#. If you're interested in the end result (including a way to download log4net nuget package), see below:
nuget_log4net.fsx:
#!/usr/bin/env fsharpi
open System
open System.IO
open System.Net
#r "System.IO.Compression.FileSystem"
open System.IO.Compression
type DummyTypeForLog4Net () =
do ()
module NetTools =
let DownloadNuget (packageId: string, packageVersion: string) =
use webClient = new WebClient()
let fileName = sprintf "%s.%s.nupkg" packageId packageVersion
let pathToUncompressTo = Path.Combine("packages", packageId)
if (Directory.Exists(pathToUncompressTo)) then
Directory.Delete(pathToUncompressTo, true)
Directory.CreateDirectory(pathToUncompressTo) |> ignore
let fileToDownload = Path.Combine(pathToUncompressTo, fileName)
let nugetDownloadUri = Uri (sprintf "https://www.nuget.org/api/v2/package/%s/%s" packageId packageVersion)
webClient.DownloadFile (nugetDownloadUri, fileToDownload)
ZipFile.ExtractToDirectory(fileToDownload, pathToUncompressTo)
let packageId = "log4net"
let packageVersion = "2.0.5"
NetTools.DownloadNuget(packageId, packageVersion)
let currentDirectory = Directory.GetCurrentDirectory()
// https://stackoverflow.com/a/19538654/6503091
#r "packages/log4net/lib/net45-full/log4net"
open log4net
open log4net.Repository.Hierarchy
open log4net.Core
open log4net.Appender
open log4net.Layout
open log4net.Config
let patternLayout = PatternLayout()
patternLayout.ConversionPattern <- "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions()
let roller = RollingFileAppender()
roller.AppendToFile <- true
roller.File <- Path.Combine(currentDirectory, "someLog.txt")
roller.Layout <- patternLayout
roller.MaxSizeRollBackups <- 5
roller.MaximumFileSize <- "1GB"
roller.RollingStyle <- RollingFileAppender.RollingMode.Size
roller.StaticLogFileName <- true
roller.ActivateOptions ()
let hierarchy = box (LogManager.GetRepository()) :?> Hierarchy
hierarchy.Root.AddAppender (roller)
hierarchy.Root.Level <- Level.Info
hierarchy.Configured <- true
BasicConfigurator.Configure(hierarchy)
let aType = typedefof<DummyTypeForLog4Net>
let logger = LogManager.GetLogger(aType)
logger.Error(new Exception("exception test"))

Format of Tracing output in System.Diagnostics.TraceSource

The following code:
static void Main(string[] args)
{
TraceSource ts = new TraceSource("MyApplication");
ts.Switch = new SourceSwitch("MySwitch");
ts.Switch.Level = SourceLevels.All;
ts.Listeners.Add(new TextWriterTraceListener(Console.Out));
ts.TraceInformation("Hello World");
Console.ReadKey();
}
generates the following output:
MyApplication Information: 0 : Hello World
The part "MyApplication Information: 0 :" at the beginning of the trace output is coming from the TraceSource class itself.
However, I need to have a timestamp at the beginning of the line and I would like to change "Information" to "Info" also.
Is there any way to get more freedom in trace output such that I can configure it to be like:
13:03:00 - MyApplication Info: Hello World
I tried for a couple of hours, but with no success. Whatever I do, at the beginning of the output line, there is always this constant predefined "MyApplication Information: 0 : Hello World" output.
MSDN documentation did also not reveal any helpful information.
Coming in late also but in case someone else lands here...
I like keeping it simple. I use one static Trace method within my App.cs which ties to a single TraceSource that I create at start up. This allows me access it throughout my app and keep the app.config simple:
public static void Trace(TraceEventType eventType, string message)
{
if (_TraceSource.Switch.ShouldTrace(eventType))
{
string tracemessage = string.Format("{0}\t[{1}]\t{2}", DateTime.Now.ToString("MM/dd/yy HH:mm:ss"), eventType, message);
foreach (TraceListener listener in _TraceSource.Listeners)
{
listener.WriteLine(tracemessage);
listener.Flush();
}
}
}
My app.config entries:
<system.diagnostics>
<sources>
<source name="mytracesource" switchValue="All">
<listeners>
<add name="mytracelistener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="trace.log">
</add>
</listeners>
</source>
</sources>
</system.diagnostics>
Set the TraceOutputOptions property on the trace listener. The format is predefined, but you can opt-in for the additional pieces of data defined by the TraceOptions enum.
TraceSource.TraceInformation ends up calling TraceListener.TraceEvent on each listener which adds in the header. Fortunately, TraceListener has a WriteLine method which you can use to produce custom output.
static void Main(string[] args)
{
TraceSource ts = new TraceSource("MyApplication");
ts.Switch = new SourceSwitch("MySwitch");
ts.Switch.Level = SourceLevels.All;
ts.Listeners.Add(new TextWriterTraceListener(Console.Out));
for (int i = 0; i < ts.Listeners.Count; i++)
{
var listener = ts.Listeners[i];
listener.WriteLine(
string.Format("{0} - {1} Info: {2}",
DateTime.Now.ToString("HH:mm:ss"), ts.Name, "Hello World"));
listener.Flush();
}
Console.ReadKey();
}
Output:
13:52:05 - MyApplication Info: Hello World
Maybe a bit late but if you want an easy and versatile solution you should have a look at the Essentials Diagnostics project on CodePlex (also available through NuGet).
It defines a wide range of listeners (with custom formatting allowed) that output to the console, rolling text and XML files, event logs, email, etc. and comes with configuration samples also.

How can I detect value syntax errors in app.config files?

I need to improve the reporting of errors in app.config files.
I have a tiny test app that includes a setting of type "int". If I change its value in app.config to something that isn't a valid integer, I'd expect an exception to get raised that I can catch and report. Unfortunately, something is eating the exception. Is there a straightforward way to prevent that behaviour and let the exception propagate out?
My Settings.Designer.cs file (generated by Visual Studio):
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(
"Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default { get { return defaultInstance; } }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("17")]
public int SecondSetting
{
get { return ((int)(this["SecondSetting"])); }
set { this["SecondSetting"] = value; }
}
}
My C# test app:
static void Main (string[] args)
{
try
{
Console.WriteLine(AppConfigTests.Properties.Settings.Default.SecondSetting);
}
catch (Exception x)
{
Console.WriteLine(x.ToString());
}
}
The relevant portion of my App.config file (note that the value isn't a valid integer):
<userSettings>
<AppConfigTests.Properties.Settings>
<setting name="SecondSetting" serializeAs="String">
<value>1foo7</value>
</setting>
</AppConfigTests.Properties.Settings>
</userSettings>
A System.Format exception is getting thrown by System.Number.StringToNumber, but something is catching it and throwing it away, and my catch block never gets entered. In the Visual Studio debugger output, I found "A first chance exception of type 'System.FormatException' occurred in mscorlib.dll", but that doesn't help me except to further confirm that an exception was thrown.
I tried adding an IntegerValidatorAttribute to my setting property in Settings.Designer.cs, but that didn't help (and I made sure the .cs didn't get regenerated).
I tried adding the following code at the top of my main() method, but that didn't help either:
foreach (SettingsProperty sp in AppConfigTests.Properties.Settings.Default.Properties)
sp.ThrowOnErrorDeserializing = true;
I've thought of implementing my own ConfigurationSection, but I'm hoping there's an easier solution.
Just to reiterate: I need a way to report errors in settings values in app.config files.
(If I break the XML syntax in App.config (by removing an angle bracket, for example), my catch block gets a nice exception with all the detail I could ask for.)
You can do it like this (assuming you have all the setting values overriden in app.config):
foreach (SettingsPropertyValue propertyValue in AppConfigTests.Properties.Settings.Default.PropertyValues) {
if (propertyValue.UsingDefaultValue) {
throw new Exception(propertyValue.Name + " is not deserialized properly.");
}
}
If you're writing web application, it is firing this event when deserialization fails:
try {
if (this.IsHostedInAspnet()) {
object[] args = new object[] { this.Property, this, ex };
Type type = Type.GetType("System.Web.Management.WebBaseEvent, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", true);
type.InvokeMember("RaisePropertyDeserializationWebErrorEvent", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, args, CultureInfo.InvariantCulture);
}
}
catch {
}
Otherwise, it just get back to default value, so only thing you can to is iterate trough all values and check if they're not defaulted.

Categories

Resources