Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

dustmouse

Member Since 03 Jan 2013
Offline Last Active Mar 18 2015 03:21 PM
-----

Posts I've Made

In Topic: A simple logger

13 March 2014 - 05:02 AM

I know this is waay late, but I was able to get a file logger working with the following code.  It can be configured to do rolling logs or just purge the log when it gets to a certain size and start filling it again.  

 

The idea is that there is only one instance of the logger (singleton) to avoid problems with different instances trying to write to the same location concurrently.  It's supposed to be thread-safe, but I haven't really tested that aspect of it yet.

    public class FileLogger : ILogger
    {
        private static FileLogger _instance;
        private static object lockObject = new Object();

        #region Constructors

        private FileLogger(string filePath, long maxFileSize, bool isRolling)
        {
            this.FilePath = filePath;
            this.MaxFileSize = maxFileSize;
            this.IsRolling = isRolling;  
        }

        #endregion

        #region Public Properties

        public static FileLogger Instance
        {
            get
            {
                if (FileLogger._instance == null)
                    throw new Exception("File logger has not been instantiated.");

                return FileLogger._instance;
            }
        }

        public static bool IsInstantiated { get; private set; }

        public string FilePath { get; private set; }

        public long MaxFileSize { get; private set; }

        public bool IsRolling { get; private set; }

        #endregion

        #region ILogger Members

        public void Log(Severity severity, string message)
        {
            if (this.FilePath == null || this.FilePath.Trim().Length == 0)
                throw new Exception("Must specify file path for logging.");

            lock (FileLogger.lockObject)
            {
                if (File.Exists(this.FilePath))
                {
                    var fileInfo = new FileInfo(this.FilePath);

                    if (this.MaxFileSize != -1 && fileInfo.Length > this.MaxFileSize)
                    {
                        if (!this.IsRolling)
                            File.Delete(this.FilePath);
                        else
                            this.rollFile();
                    }
                }

                using (var fileStream = new FileStream(this.FilePath, FileMode.Append))
                using (var streamWriter = new StreamWriter(fileStream))
                    streamWriter.WriteLine(DateTime.Now.ToString() + " - " +
                        severity.ToString().ToUpper() + " - " + message);
            }
        }

        #endregion

        #region Public Methods

        public static FileLogger Instantiate(
            string filePath, long maxFileSize = -1, bool isRolling = false)
        {
            if (FileLogger.IsInstantiated)
                throw new Exception("File logger has already been instantiated.");

            lock (FileLogger.lockObject)
            {
                FileLogger._instance =
                    new FileLogger(filePath, maxFileSize, isRolling);

                FileLogger.IsInstantiated = true;
            }

            return FileLogger._instance;
        }

        #endregion

        #region Private Methods

        private void rollFile()
        {
            if (!File.Exists(this.FilePath))
                throw new Exception(
                    "Failed to roll log file.  '" + this.FilePath + "' does not exist.");

            int fileExtensionIndex = this.FilePath.LastIndexOf('.');
            string rolledFilePath = this.FilePath.Substring(0, fileExtensionIndex) +
                "_" + Guid.NewGuid().ToString() + 
                this.FilePath.Substring(fileExtensionIndex, this.FilePath.Length - fileExtensionIndex);
            
            File.Move(this.FilePath, rolledFilePath);
        }

        #endregion
    }

In Topic: Clean way to interrupt Socket.Accept()?

16 January 2014 - 04:53 AM

Looks like that's exactly what I needed, thanks.  As you suggested, I changed it around a little and currently have it like this:

while (!this.interrupt)                    if (this.socket.Poll(POLL_TIMEOUT, SelectMode.SelectRead))                        using (Socket connection = this.socket.Accept())

Does this look like a good way to structure it?


In Topic: Error(?) using WebRequest

01 January 2014 - 09:51 PM

Does it just hang when it hits the GetResponseStream() line or throw an exception?  Have you tried adjusting the Timeout property on the WebRequest to see if there is an issue with it timing out?  Also, how big of a response are you expecting?  If it's really big, could it be flooding the device's memory?


In Topic: uning List<struct>

22 December 2013 - 09:34 PM

Without generics, I like to create my own collection class to ensure type safety by including methods for adding and removing objects of a certain type.  The class uses an ArrayList as its internal collection.  The nice thing is that you can ensure that all of the items in the collection are of the expected type.  


In Topic: Code C# netduino plus with multiple control led

22 December 2013 - 09:29 PM

Do you need to set multiple LEDs to the same state?  How do you want the server to interact with the LEDs?


home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.