2011-11-05 57 views
5

Phonegap v1.1.0,如何從console.log(字符串)訪問輸出?Phonegap WP7 Visual Studio 2010 console.log

// provide our own console if it does not exist, huge dev aid! 
if(typeof window.console == "undefined") 
{ 
window.console = {log:function(str){window.external.Notify(str);}}; 
} 

// output any errors to console log, created above. 
window.onerror=function(e){console.log("Error ::" + e);}; 

console.log("Installed console ! "); 

它記錄到調試輸出窗口

+0

Thanx !!這幫助我解決了一些惱人的問題:) – VDP

回答

1

的console.log定義如下

if(typeof window.console == "undefined") 
{ 
    window.console = { 
     log:function(str){ 
      if(navigator.debugConsole){ 
       navigator.debugConsole.log(str); 
      } 
      else 
      {// In case log messages are received before device ready 
       window.external.Notify("Info:" + str); 
      } 
     } 
    }; 
} 

兩個debugConsole.log()和window.external.Notify結果( )是Debug.WriteLine(msg)方法調用。因此可能您可以將調試輸出重定向到例如文件並堅持這些信息以便稍後能夠調試/查看此信息。不需要連接到VS來調試問題,有時它可能非常有幫助,代碼示例

TextWriterTraceListener[] listeners = new TextWriterTraceListener[] 
{ 
    new TextWriterTraceListener("debug.log"), 
    new TextWriterTraceListener(Console.Out) 
}; 

Debug.Listeners.AddRange(listeners);