Пример #1
0
        public void AddTemplate(EventSourceLoggerTemplate loggerTemplate)
        {
            var events = new List <EventSourceEvent>();

            this.LoggerNamespace = loggerTemplate.Namespace;
            foreach (var templateEvent in loggerTemplate.Events)
            {
                events.Add(templateEvent);
            }
            this.Events  = events.ToArray();
            this.Include = loggerTemplate.Include;
        }
        private EventSourceLoggerTemplate[] CompileAndEvaluateInterface(ProjectItem projectItem)
        {
            LogMessage($"Compiling possible logger file {projectItem.Include}");

            var loggers = new List <EventSourceLoggerTemplate>();

            try
            {
                var parameters = new CompilerParameters();

                parameters.ReferencedAssemblies.Add("System.dll");
                parameters.GenerateExecutable = false;
                parameters.GenerateInMemory   = true;

                parameters.IncludeDebugInformation = false;
                var cSharpCodeProvider = new CSharpCodeProvider();
                //var cSharpCodeProvider = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
                var compilerResults = cSharpCodeProvider.CompileAssemblyFromFile(parameters, projectItem.Name);
                foreach (CompilerError compilerResultsError in compilerResults.Errors)
                {
                    LogMessage(compilerResultsError.ToString());
                }

                var types = compilerResults.CompiledAssembly.GetTypes();
                foreach (
                    var type in
                    types.Where(t => t.IsInterface && t.Name.Matches(@"^I[^\\]*Logger", StringComparison.InvariantCultureIgnoreCase, useWildcards: false)))
                {
                    var include           = projectItem.Include.Replace(projectItem.Name, type.Name);
                    var eventSourceLogger = new EventSourceLoggerTemplate()
                    {
                        Name      = type.Name,
                        Namespace = type.Namespace,
                        Include   = include
                    };
                    var eventSourceEvents = new List <EventSourceEvent>();
                    foreach (var methodInfo in type.GetMethods())
                    {
                        var eventSourceEventArguments = new List <EventSourceEventArgument>();
                        var eventSourceEvent          = new EventSourceEvent()
                        {
                            Name = methodInfo.Name,
                        };
                        foreach (var parameterInfo in methodInfo.GetParameters())
                        {
                            var typeString = parameterInfo.ParameterType.GetFriendlyName();
                            eventSourceEventArguments.Add(new EventSourceEventArgument()
                            {
                                Name = parameterInfo.Name,
                                Type = typeString,
                            });
                        }
                        eventSourceEvent.Arguments = eventSourceEventArguments.ToArray();
                        eventSourceEvents.Add(eventSourceEvent);
                    }

                    eventSourceLogger.Events = eventSourceEvents.ToArray();
                    loggers.Add(eventSourceLogger);
                }
            }
            catch (Exception ex)
            {
                LogMessage($"Failed to compile/evaluate {projectItem.Include} - {ex.Message}\r\n{ex.StackTrace}");
            }
            return(loggers.ToArray());
        }