diff --git a/XrmToolBox.Extensibility/LogManager.cs b/XrmToolBox.Extensibility/LogManager.cs index 089d7d73..99b6a15e 100644 --- a/XrmToolBox.Extensibility/LogManager.cs +++ b/XrmToolBox.Extensibility/LogManager.cs @@ -66,6 +66,16 @@ public void LogInfo(string message, params object[] args) Log(Level.Info, args.Length == 0 ? message : string.Format(message, args)); } + /// + /// Writes a verbose message in the log + /// + /// + /// + public void LogVerbose(string message, params object[] args) + { + Log(Level.Verbose, args.Length == 0 ? message : string.Format(message, args)); + } + /// /// Writes a warning message in the log /// @@ -112,6 +122,10 @@ public void SetConnection(ConnectionDetail connectionDetail) /// Content of the message private void Log(Level level, string message) { + if (LogLevel > level) + { // No logging for this level since it's less than the settings level + return; + } var parentFolder = Path.GetDirectoryName(_filePath); if (parentFolder != null && !Directory.Exists(parentFolder)) { @@ -124,7 +138,7 @@ private void Log(Level level, string message) { using (StreamWriter writer = new StreamWriter(_filePath, true)) { - writer.WriteLine("{0:yyyy-MM-dd hh:mm:ss.fff tt}\t{1}\t{2}\t{3}", DateTime.Now, + writer.WriteLine("{0:yyyy-MM-dd HH:mm:ss.fff}\t{1}\t{2}\t{3}", DateTime.Now, _connection?.ConnectionName, level, message); } } diff --git a/XrmToolBox.Extensibility/PluginControlBase.cs b/XrmToolBox.Extensibility/PluginControlBase.cs index b030aab2..d1f24179 100644 --- a/XrmToolBox.Extensibility/PluginControlBase.cs +++ b/XrmToolBox.Extensibility/PluginControlBase.cs @@ -390,6 +390,16 @@ public void LogInfo(string message, params object[] args) logManager.LogInfo(message, args); } + /// + /// Writes a verbose message in the log + /// + /// + /// + public void LogVerbose(string message, params object[] args) + { + logManager.LogVerbose(message, args); + } + /// /// Writes a warning message in the log /// diff --git a/XrmToolBox/AppCode/AppInsights/AppInsights.cs b/XrmToolBox/AppCode/AppInsights/AppInsights.cs index 16550ae8..fda0e7e0 100644 --- a/XrmToolBox/AppCode/AppInsights/AppInsights.cs +++ b/XrmToolBox/AppCode/AppInsights/AppInsights.cs @@ -27,8 +27,8 @@ using System.Reflection; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; -using System.Text; using XrmToolBox.AppCode.AppInsights; +using XrmToolBox.Extensibility; public static class Extensions { @@ -82,6 +82,7 @@ private static string GetLastDotPart(string identifier) public class AppInsights { + private readonly PluginControlBase _tool; private readonly AiConfig _aiConfig; private int seq = 1; @@ -93,6 +94,21 @@ public class AppInsights /// Instrumentation Key for the AppInsights instance in the Azure portal /// Assembly info to include in logging, usually pass Assembly.GetExecutingAssembly() /// Override name of the tool, defaults to last part of the logging assembly name + public AppInsights(PluginControlBase tool, string endpoint, string ikey, Assembly loggingassembly, string toolname = null) + { + _tool = tool; + _aiConfig = new AiConfig(endpoint, ikey, loggingassembly, toolname); + } + + /// + /// Initializes Application Insights instance. + /// When called from a tool, make sure to pass Assembly.GetExecutingAssembly() as loggingassembly parameter!! + /// + /// AppInsights endpoint, usually https://dc.services.visualstudio.com/v2/track + /// Instrumentation Key for the AppInsights instance in the Azure portal + /// Assembly info to include in logging, usually pass Assembly.GetExecutingAssembly() + /// Override name of the tool, defaults to last part of the logging assembly name + [Obsolete("Use constructor accepting PluginControlBase tool, endpoint, ikey, loggingassembly and toolname instead.", false)] public AppInsights(string endpoint, string ikey, Assembly loggingassembly, string toolname = null) { _aiConfig = new AiConfig(endpoint, ikey, loggingassembly, toolname); @@ -110,6 +126,7 @@ public AppInsights(AiConfig aiConfig) public void WriteEvent(string eventName, double? count = null, double? duration = null, Action resultHandler = null) { + _tool?.LogVerbose($"{eventName}{(count != null ? $" Count: {count}" : "")}{(duration != null ? $" Duration: {duration}" : "")}"); if (!_aiConfig.LogEvents) return; var logRequest = GetLogRequest("Event"); logRequest.Data.BaseData.Name = eventName;