Skip to content

Instantly share code, notes, and snippets.

@alex-doe
Last active June 24, 2016 09:42
Show Gist options
  • Save alex-doe/58ae79b78816d6a33956b4014e055190 to your computer and use it in GitHub Desktop.
Save alex-doe/58ae79b78816d6a33956b4014e055190 to your computer and use it in GitHub Desktop.
LogException in Interval Class
// The MIT License(MIT)
//Copyright(c) 2016 alex-doe
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace TestExceptionClass {
class Program {
static void Main(string[] args) {
var exceptionInTimeHandler = new ExceptionInTimeHandler(TimeSpan.FromSeconds(60));
DateTime starttime = DateTime.UtcNow;
while (true) {
try {
//throw every cycle
DirectoryInfo info = new DirectoryInfo("E:\\Temp");
info.GetFiles();
} catch (Exception ex) {
exceptionInTimeHandler.LogException(ex);
}
try {
//throw every 4 seconds
if (starttime.AddSeconds(4) <= DateTime.UtcNow) {
var infoexalle2 = new DirectoryInfo("C:\\Tempasdfasdf");
infoexalle2.GetFiles();
}
} catch (Exception ex) {
exceptionInTimeHandler.LogException(ex);
}
try {
//throw every 7 seconds
if (starttime.AddSeconds(7) <= DateTime.UtcNow) {
throw new NotImplementedException("Test impl ex 1");
}
} catch (Exception ex) {
exceptionInTimeHandler.LogException(ex);
}
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
}
public class ExceptionInTimeHandler {
private readonly List<TimedExceptionEntry> _exceptionEntries;
private readonly TimeSpan _logInterval;
public ExceptionInTimeHandler(TimeSpan logInterval) {
_logInterval = logInterval;
_exceptionEntries = new List<TimedExceptionEntry>();
}
public void LogException(Exception ex) {
//todo optimize compare
if (_exceptionEntries.Any(e => e.Exception.StackTrace == ex.StackTrace) &&
_exceptionEntries.Any(e => e.Exception.Message == ex.Message)) {
var exception = _exceptionEntries.Single(e => e.Exception.StackTrace == ex.StackTrace && e.Exception.Message == ex.Message);
if (exception.LastLogTime.Add(_logInterval) > DateTime.UtcNow) return;
exception.LastLogTime = DateTime.UtcNow;
} else {
_exceptionEntries.Add(new TimedExceptionEntry(ex));
}
Logger.Instance.LogFormat(ex.ToString());
}
private class TimedExceptionEntry {
public Exception Exception { get; set; }
public DateTime LastLogTime { get; set; }
public TimedExceptionEntry(Exception exception) {
Exception = exception;
LastLogTime = DateTime.UtcNow;
}
}
}
//fake logger
public class Logger {
private static Logger _logger;
public static Logger Instance => _logger ?? (_logger = new Logger());
public void LogFormat(string text) {
Console.WriteLine(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment