Silverlight Logging framework and/or best practices

前端 未结 6 1242
谎友^
谎友^ 2020-12-05 02:37

Now that Silverlight 2 has finally shipped. I\'m wondering if anyone has put together any logging frameworks for it, maybe something like enterprise library logging or log4n

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 03:37

    if you just want to output debug messages to the console. You could use the Browser's console.log mechanism. I coded a extension method for that. You can find on my blog.

        // http://kodierer.blogspot.com.es/2009/05/silverlight-logging-extension-method.html
        public static string Log(string message)
        {
            var msgLog = "";
            try
            {
    
                HtmlWindow window = HtmlPage.Window;
    
                //only log if a console is available
                var isConsoleAvailable = (bool)window.Eval("typeof(console) != 'undefined' && typeof(console.log) != 'undefined'");
    
                if (!isConsoleAvailable) return "isConsoleAvailable " + isConsoleAvailable;
    
                var createLogFunction = (bool)window.Eval("typeof(ssplog) == 'undefined'");
                if (createLogFunction)
                {
                    // Load the logging function into global scope:
                    string logFunction = @"function ssplog(msg) { console.log(msg); }";
                    string code = string.Format(@"if(window.execScript) {{ window.execScript('{0}'); }} else {{ eval.call(null, '{0}'); }}", logFunction);
                    window.Eval(code);
                }
    
                // Prepare the message
                DateTime dateTime = DateTime.Now;
                string output = string.Format("{0} - {1} - {2}", dateTime.ToString("u"), "DEBUG", message);
    
                // Invoke the logging function:
                var logger = window.Eval("ssplog") as ScriptObject;
                logger.InvokeSelf(output);
            }
            catch (Exception ex)
            {
                msgLog = "Error Log " + ex.Message;
            }
            return msgLog;
    
        }
    

提交回复
热议问题