Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added POC for ignore exception types #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store
.vs/
19 changes: 18 additions & 1 deletion Log4Slack/SlackAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class SlackAppender : AppenderSkeleton {
private readonly Process _currentProcess = Process.GetCurrentProcess();
private List<Mapping> Mappings = new List<Mapping>();

public static List<Type> ExceptionTypesToIgnore = new List<Type>();

/// <summary>
/// Slack token.
/// https://api.slack.com/
Expand Down Expand Up @@ -79,7 +81,12 @@ public class SlackAppender : AppenderSkeleton {

public Mapping mapping { set { Mappings.Add(value); } }

protected override void Append(log4net.Core.LoggingEvent loggingEvent) {
protected override void Append(log4net.Core.LoggingEvent loggingEvent)
{

if (ExceptionTypesToIgnore.Contains(loggingEvent.ExceptionObject.GetType()))
return;

// Initialze the Slack client
var slackClient = new SlackClient(WebhookUrl.Expand());
var attachments = new List<Attachment>();
Expand Down Expand Up @@ -140,6 +147,16 @@ protected override void Append(log4net.Core.LoggingEvent loggingEvent) {
}
}


public static class PublicExtensions
{
public static void Add<T>(this List<Type> list)
{
list.Add(typeof(T));
}
}


internal static class Extensions {
public static string Expand(this string text) {
return text != null ? Environment.ExpandEnvironmentVariables(text) : null;
Expand Down
12 changes: 12 additions & 0 deletions Log4SlackTesting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
using System.Text;
using System.Threading.Tasks;
using log4net;
using Log4Slack;

namespace Log4SlackTesting {
class Program {
static void Main(string[] args) {
log4net.Config.XmlConfigurator.Configure();

// Add exception to ignore
SlackAppender.ExceptionTypesToIgnore.Add(typeof(UnauthorizedAccessException));

// Or add exception to ignore using extension
SlackAppender.ExceptionTypesToIgnore.Add<InvalidCastException>();

var logger = LogManager.GetLogger(typeof(Program));
logger.Info("I know he can get the job, but can he do the job?");
logger.Debug("I'm not arguing that with you.");
Expand All @@ -23,7 +31,11 @@ static void Main(string[] args) {

logger.Fatal("That's it. It's over.", new EncoderFallbackException("Could not fall backwards."));

logger.Error("This should be ignored.", new UnauthorizedAccessException("No."));
logger.Error("This as well.", new InvalidCastException("No."));

Console.ReadKey();
}
}
}