Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Imports System.Windows.Forms Public Module LoggingInjection Const BeginCode As String = "using (Logger logger = new Logger(typeof({1}), ""{0}""))" + vbCrLf + "{{" Const EndCode As String = vbCrLf + "}" Sub InjectLoggingInClass(ByVal classElement As CodeClass2) Dim element As CodeElement For Each element In classElement.Members If (element.Kind = vsCMElement.vsCMElementFunction) Then Dim codeFunction As CodeFunction codeFunction = CType(element, CodeFunction2) InjectLoggingInFunction(codeFunction, classElement.Name) End If If (element.Kind = vsCMElement.vsCMElementProperty) Then Dim codeProperty As CodeProperty codeProperty = CType(element, CodeProperty) InjectLoggingInFunction(codeProperty.Getter, classElement.Name, "get_" + codeProperty.Name) InjectLoggingInFunction(codeProperty.Setter, classElement.Name, "set_" + codeProperty.Name) End If Next 'DTE.ActiveDocument.Selection.SelectAll() 'DTE.ExecuteCommand("Edit.FormatSelection") End Sub Sub InjectLoggingInFunction(ByVal functionElement As CodeFunction, ByVal typeName As String, Optional ByVal name As String = Nothing) 'functionElement.FunctionKind If (name Is Nothing) Then name = functionElement.Name End If Try Dim startPoint As TextPoint startPoint = functionElement.GetStartPoint(vsCMPart.vsCMPartBody) Dim endPoint As TextPoint endPoint = functionElement.GetEndPoint(vsCMPart.vsCMPartBody) Dim editPoint As EditPoint editPoint = startPoint.CreateEditPoint() editPoint.Insert(String.Format(BeginCode, name, typeName)) editPoint = endPoint.CreateEditPoint() editPoint.Insert(EndCode) Catch ex As Exception End Try End Sub 'Sub InjectLoggingInProject() ' Dim project As Project ' Dim projectItem As ProjectItem ' project = DTE.ActiveDocument.ProjectItem.ContainingProject ' For Each projectItem In project.ProjectItems ' If (Not projectItem.FileCodeModel Is Nothing) Then ' Dim window As Window ' window = projectItem.Open() ' InjectLoggingInFileCodeModel(projectItem.FileCodeModel) ' End If ' Next 'End Sub Sub InjectLoggingInFileCodeModel(ByVal fileCodeModel As FileCodeModel) DTE.UndoContext.Open("Inject logging") Dim codeElement As CodeElement Dim classElement As CodeClass Dim IsInjectedCode As Boolean = False Try For Each codeElement In fileCodeModel.CodeElements If (codeElement.Kind = vsCMElement.vsCMElementNamespace) Then Dim namespaceElement As CodeNamespace namespaceElement = CType(codeElement, CodeNamespace) For Each namespaceMemberElement As CodeElement In namespaceElement.Members If (namespaceMemberElement.Kind = vsCMElement.vsCMElementClass) Then classElement = CType(namespaceMemberElement, CodeClass) IsInjectedCode = True InjectLoggingInClass(classElement) End If Next End If Next DTE.UndoContext.Close() Catch ex As Exception DTE.UndoContext.SetAborted() MessageBox.Show(ex.ToString()) End Try End Sub Sub InjectLoggingInCurrentType() DTE.UndoContext.Open("Inject logging") Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection Try Dim classElement As CodeClass2 = selection.ActivePoint.CodeElement(vsCMElement.vsCMElementClass) If (classElement Is Nothing) Then Throw New ApplicationException("Could not find enclosing class for the selection") End If InjectLoggingInClass(classElement) DTE.UndoContext.Close() Catch ex As Exception DTE.UndoContext.SetAborted() MessageBox.Show(ex.ToString()) End Try End Sub Sub InjectLoggingInCurrentFile() Dim activeProjectItem As ProjectItem = DTE.ActiveWindow.ProjectItem InjectLoggingInProjectItem(activeProjectItem) End Sub Sub OpenAllSubItems() Dim activeProject As Project = DTE.ActiveSolutionProjects(0) Dim cmdWindow As Window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow) OpenProjectItems(activeProject.ProjectItems()) End Sub Sub OpenProjectItems(ByVal projectItems As ProjectItems) Dim projectItem As ProjectItem For Each projectItem In projectItems '''My Code starts If (Not projectItem.FileCodeModel Is Nothing) Then Dim window As Window window = projectItem.Open(Constants.vsViewKindTextView) window.Visible = True window.Activate() DTE.ExecuteCommand("Edit.Find") DTE.Find.FindWhat = "using Common.Utils;" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = True DTE.Find.MatchWholeWord = True DTE.Find.Backwards = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.Action = vsFindAction.vsFindActionFind If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then Dim objTextDoc As TextDocument Dim objEP As EditPoint objTextDoc = DTE.ActiveDocument.Object("TextDocument") objEP = objTextDoc.StartPoint.CreateEditPoint objEP.Insert("using Common.Utils;" + vbCrLf) End If DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesYes) End If '''My code ends ' Recurse if the project item has sub-items... Dim projectSubItems As ProjectItems projectSubItems = projectItem.ProjectItems Dim doHaveSubItems As Boolean = Not projectSubItems Is Nothing If doHaveSubItems Then OpenProjectItems(projectSubItems) End If Next End Sub Sub InjectLoggingInActiveProject() Dim activeProject As Project = DTE.ActiveSolutionProjects(0) Dim cmdWindow As Window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow) InjectLoggingInProjectItems(activeProject.ProjectItems()) End Sub Sub InjectLoggingInProjectItem(ByVal projectItem As ProjectItem) If (Not projectItem.FileCodeModel Is Nothing) Then Dim window As Window window = projectItem.Open(Constants.vsViewKindCode) window.Visible = True window.Activate() InjectLoggingInFileCodeModel(projectItem.FileCodeModel) DTE.ActiveDocument.Selection.SelectAll() DTE.ExecuteCommand("Edit.FormatSelection") DTE.ExecuteCommand("Edit.Find") DTE.Find.FindWhat = "using EM.Common.Utils;" DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument DTE.Find.MatchCase = True DTE.Find.MatchWholeWord = True DTE.Find.Backwards = False DTE.Find.MatchInHiddenText = True DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral DTE.Find.Action = vsFindAction.vsFindActionFind If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then Dim objTextDoc As TextDocument Dim objEP As EditPoint objTextDoc = DTE.ActiveDocument.Object("TextDocument") objEP = objTextDoc.StartPoint.CreateEditPoint objEP.Insert("using EM.Common.Utils;" + vbCrLf) End If Dim findWindow As Window = DTE.Windows.Item(Constants.vsWindowKindFindReplace) findWindow.Close(vsSaveChanges.vsSaveChangesNo) End If End Sub Sub InjectLoggingInProjectItems(ByVal projectItems As ProjectItems) Dim projectItem As ProjectItem For Each projectItem In projectItems '''My Code starts InjectLoggingInProjectItem(projectItem) '''My code ends ' Recurse if the project item has sub-items... Dim projectSubItems As ProjectItems projectSubItems = projectItem.ProjectItems Dim doHaveSubItems As Boolean = Not projectSubItems Is Nothing If doHaveSubItems Then InjectLoggingInProjectItems(projectSubItems) End If Next End Sub End Module