Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Server Development
Habbo Retros
Habbo Releases
Minimalist logging for Habbo emulators
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="c4353b8e" data-source="post: 434663" data-attributes="member: 81116"><p>I know a lot of emulators use the package log4net & a few others but I've always seen it as overload, especially how emulators never really get into using even half of the functionality of that package. I put together a simple clone of what NLog does in a single class.</p><p></p><p>If anybody likes writing their own code, or just need something minimal, here you go. It could be written a lot better but what couldn't, it serves its purpose & I'm happy with the code quality it's at. You probably don't need the File.Exists part inside the final method, but I just put it there for safety. Id recommend throwing an interface on this too so you can have multiple types of ILogger</p><p></p><p>[CODE]</p><p>public class ConsoleLogger <TClass> : ILogger, IDisposable</p><p>{</p><p> private readonly string _filePath;</p><p> private readonly Type _className;</p><p> private readonly Dictionary<string, StreamWriter> _streams;</p><p></p><p> public ConsoleLogger()</p><p> {</p><p> _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + "/resources/logging/");</p><p> _className = typeof(TClass);</p><p> _streams = new Dictionary<string, StreamWriter>();</p><p> }</p><p></p><p> private readonly Dictionary<LogType, string> _fileForLogType = new Dictionary<LogType, string> {</p><p> {LogType.Success , "trace.log"},</p><p> {LogType.Warn , "error.log"},</p><p> {LogType.Error , "error.log"},</p><p> {LogType.Debug , "debug.log"},</p><p> };</p><p></p><p> private readonly Dictionary<LogType, ConsoleColor> _colorForLogType = new Dictionary<LogType, ConsoleColor> {</p><p> {LogType.Success , ConsoleColor.Green},</p><p> {LogType.Warn , ConsoleColor.Yellow},</p><p> {LogType.Error , ConsoleColor.Red},</p><p> {LogType.Debug , ConsoleColor.Cyan},</p><p> };</p><p></p><p> public void Trace(string message, bool logToFile = false)</p><p> {</p><p> Log(message, LogType.Trace, logToFile);</p><p> }</p><p></p><p> public void Warn(string message, bool logToFile = false)</p><p> {</p><p> Log(message, LogType.Warn, logToFile);</p><p> }</p><p></p><p> public void Debug(string message, bool logToFile = false)</p><p> {</p><p> Log(message, LogType.Debug, logToFile);</p><p> }</p><p></p><p> public void Success(string message, bool logToFile = false)</p><p> {</p><p> Log(message, LogType.Success, logToFile);</p><p> }</p><p></p><p> public void Error(string message, bool logToFile = false)</p><p> {</p><p> Log(message, LogType.Error, logToFile);</p><p> }</p><p></p><p> public void Error(Exception e, bool logToFile = true)</p><p> {</p><p> Log("An error occurred: " + Environment.NewLine + e, LogType.Error, logToFile);</p><p> }</p><p></p><p> private void Log(string message, LogType logType, bool logToFile = false)</p><p> {</p><p> var oldColor = Console.ForegroundColor;</p><p></p><p> Console.ForegroundColor = _colorForLogType[logType];</p><p> Console.WriteLine($"[{DateTime.Now:MM/dd HH:mm:ss}] " + message);</p><p> Console.ForegroundColor = oldColor;</p><p> </p><p> if (logToFile)</p><p> {</p><p> LogToFile(_fileForLogType[logType], $"Occurred at [{DateTime.Now:MM/dd HH:mm:ss}] in [{_className.FullName}]: {message}");</p><p> }</p><p> }</p><p> </p><p> private void LogToFile(string file, string content)</p><p> {</p><p> var fullPath = Path.Combine(_filePath, file);</p><p></p><p> if (!Directory.Exists(_filePath))</p><p> {</p><p> Directory.CreateDirectory(_filePath);</p><p> }</p><p></p><p> if (!File.Exists(fullPath))</p><p> {</p><p> File.Create(fullPath);</p><p> }</p><p></p><p> if (!_streams.ContainsKey(file))</p><p> {</p><p> _streams.Add(file, new StreamWriter(_filePath + file, true));</p><p> }</p><p></p><p> _streams[file].WriteLine(content);</p><p> }</p><p></p><p> public void Dispose()</p><p> {</p><p> foreach (var stream in _streams.Values)</p><p> {</p><p> stream.Dispose();</p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p><p></p><p>Update: Improved code quality, cached streams, & better logic for getting the files & colors via log type.</p><p></p><p>Here are the other files.</p><p>[CODE]</p><p>public enum LogType</p><p>{</p><p> Trace,</p><p> Warn,</p><p> Debug,</p><p> Success,</p><p> Error</p><p>}</p><p>[/CODE]</p><p></p><p>& the interface</p><p>[CODE]</p><p>public interface ILogger</p><p>{</p><p> void Trace(string message, bool logToFile = false);</p><p> void Warn(string message, bool logToFile = false);</p><p> void Debug(string message, bool logToFile = false);</p><p> void Success(string message, bool logToFile = false);</p><p> void Error(string message, bool logToFile = false);</p><p> void Error(Exception e, bool logToFile = true);</p><p>}</p><p>[/CODE]</p><p></p><p>Here's some usage, using statement for instant disposal.</p><p>[CODE]</p><p>using (var logger = new ConsoleLogger(typeof(Program)))</p><p>{</p><p> logger.Debug("test");</p><p> logger.Warn("test");</p><p> logger.Trace("test");</p><p> logger.Success("test");</p><p> logger.Error("test", true);</p><p>}</p><p>[/CODE]</p><p></p><p>Don't do the using approach in production, it'll close the streams instantly and the caching is useless. Create a property for each class that requires to log and you can use it throughout its lifetime.</p></blockquote><p></p>
[QUOTE="c4353b8e, post: 434663, member: 81116"] I know a lot of emulators use the package log4net & a few others but I've always seen it as overload, especially how emulators never really get into using even half of the functionality of that package. I put together a simple clone of what NLog does in a single class. If anybody likes writing their own code, or just need something minimal, here you go. It could be written a lot better but what couldn't, it serves its purpose & I'm happy with the code quality it's at. You probably don't need the File.Exists part inside the final method, but I just put it there for safety. Id recommend throwing an interface on this too so you can have multiple types of ILogger [CODE] public class ConsoleLogger <TClass> : ILogger, IDisposable { private readonly string _filePath; private readonly Type _className; private readonly Dictionary<string, StreamWriter> _streams; public ConsoleLogger() { _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + "/resources/logging/"); _className = typeof(TClass); _streams = new Dictionary<string, StreamWriter>(); } private readonly Dictionary<LogType, string> _fileForLogType = new Dictionary<LogType, string> { {LogType.Success , "trace.log"}, {LogType.Warn , "error.log"}, {LogType.Error , "error.log"}, {LogType.Debug , "debug.log"}, }; private readonly Dictionary<LogType, ConsoleColor> _colorForLogType = new Dictionary<LogType, ConsoleColor> { {LogType.Success , ConsoleColor.Green}, {LogType.Warn , ConsoleColor.Yellow}, {LogType.Error , ConsoleColor.Red}, {LogType.Debug , ConsoleColor.Cyan}, }; public void Trace(string message, bool logToFile = false) { Log(message, LogType.Trace, logToFile); } public void Warn(string message, bool logToFile = false) { Log(message, LogType.Warn, logToFile); } public void Debug(string message, bool logToFile = false) { Log(message, LogType.Debug, logToFile); } public void Success(string message, bool logToFile = false) { Log(message, LogType.Success, logToFile); } public void Error(string message, bool logToFile = false) { Log(message, LogType.Error, logToFile); } public void Error(Exception e, bool logToFile = true) { Log("An error occurred: " + Environment.NewLine + e, LogType.Error, logToFile); } private void Log(string message, LogType logType, bool logToFile = false) { var oldColor = Console.ForegroundColor; Console.ForegroundColor = _colorForLogType[logType]; Console.WriteLine($"[{DateTime.Now:MM/dd HH:mm:ss}] " + message); Console.ForegroundColor = oldColor; if (logToFile) { LogToFile(_fileForLogType[logType], $"Occurred at [{DateTime.Now:MM/dd HH:mm:ss}] in [{_className.FullName}]: {message}"); } } private void LogToFile(string file, string content) { var fullPath = Path.Combine(_filePath, file); if (!Directory.Exists(_filePath)) { Directory.CreateDirectory(_filePath); } if (!File.Exists(fullPath)) { File.Create(fullPath); } if (!_streams.ContainsKey(file)) { _streams.Add(file, new StreamWriter(_filePath + file, true)); } _streams[file].WriteLine(content); } public void Dispose() { foreach (var stream in _streams.Values) { stream.Dispose(); } } } [/CODE] Update: Improved code quality, cached streams, & better logic for getting the files & colors via log type. Here are the other files. [CODE] public enum LogType { Trace, Warn, Debug, Success, Error } [/CODE] & the interface [CODE] public interface ILogger { void Trace(string message, bool logToFile = false); void Warn(string message, bool logToFile = false); void Debug(string message, bool logToFile = false); void Success(string message, bool logToFile = false); void Error(string message, bool logToFile = false); void Error(Exception e, bool logToFile = true); } [/CODE] Here's some usage, using statement for instant disposal. [CODE] using (var logger = new ConsoleLogger(typeof(Program))) { logger.Debug("test"); logger.Warn("test"); logger.Trace("test"); logger.Success("test"); logger.Error("test", true); } [/CODE] Don't do the using approach in production, it'll close the streams instantly and the caching is useless. Create a property for each class that requires to log and you can use it throughout its lifetime. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Server Development
Habbo Retros
Habbo Releases
Minimalist logging for Habbo emulators
Top