public async Task Invoke(HttpContext context)
 {
     try
     {
         await _next(context);
     }
     catch (Exception ex)
     {
         _logger.LogError("An unhandled exception has occurred: "
                          + ex.Message, ex);
         throw; // Don't stop the error
     }
 }
Esempio n. 2
0
        public void Main(string[] args)
        {
            _logger.LogInformation("Starting");

            var startTime = DateTimeOffset.UtcNow;

            _logger.LogInformation(1, "Started at '{StartTime}' and 0x{Hello:X} is hex of 42", startTime, 42);

            try
            {
                throw new Exception("Boom");
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Unexpected critical error starting application", ex);
                _logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null);
                // This write should not log anything
                _logger.Log(LogLevel.Critical, 0, null, null, null);
                _logger.LogError("Unexpected error", ex);
                _logger.LogWarning("Unexpected warning", ex);
            }

            using (_logger.BeginScopeImpl("Main"))
            {
                Console.WriteLine("Hello World");

                _logger.LogInformation("Waiting for user input");
                Console.ReadLine();
            }

            var endTime = DateTimeOffset.UtcNow;

            _logger.LogInformation(2, "Stopping at '{StopTime}'", endTime);

            _logger.LogInformation("Stopping");

            _logger.LogInformation(Environment.NewLine);
            _logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
            _logger.LogInformation("{Result,-10}{StartTime,15}{EndTime,15}{Duration,15}", "------", "----- ----", "--- ----", "------------");
            _logger.LogInformation("{Result,-10}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
        }
Esempio n. 3
0
        public async Task <RestoreResult> ExecuteAsync(RestoreRequest request)
        {
            if (request.Project.TargetFrameworks.Count == 0)
            {
                _log.LogError("The project does not specify any target frameworks!");
                return(new RestoreResult(success: false, restoreGraphs: Enumerable.Empty <RestoreTargetGraph>()));
            }

            var projectLockFilePath = string.IsNullOrEmpty(request.LockFilePath) ?
                                      Path.Combine(request.Project.BaseDirectory, LockFileFormat.LockFileName) :
                                      request.LockFilePath;

            _log.LogInformation($"Restoring packages for '{request.Project.FilePath}'");

            _log.LogWarning("TODO: Read and use lock file");

            // Load repositories
            var projectResolver = new PackageSpecResolver(request.Project.BaseDirectory);
            var nugetRepository = Repository.Factory.GetCoreV3(request.PackagesDirectory);

            var context = new RemoteWalkContext();

            context.ProjectLibraryProviders.Add(
                new LocalDependencyProvider(
                    new PackageSpecReferenceDependencyProvider(projectResolver)));

            if (request.ExternalProjects != null)
            {
                context.ProjectLibraryProviders.Add(
                    new LocalDependencyProvider(
                        new ExternalProjectReferenceDependencyProvider(request.ExternalProjects)));
            }

            context.LocalLibraryProviders.Add(
                new SourceRepositoryDependencyProvider(nugetRepository, _log));

            foreach (var provider in request.Sources.Select(s => CreateProviderFromSource(s, request.NoCache)))
            {
                context.RemoteLibraryProviders.Add(provider);
            }

            var remoteWalker = new RemoteDependencyWalker(context);

            var projectRange = new LibraryRange()
            {
                Name           = request.Project.Name,
                VersionRange   = new VersionRange(request.Project.Version),
                TypeConstraint = LibraryTypes.Project
            };

            // Resolve dependency graphs
            var frameworks = request.Project.TargetFrameworks.Select(f => f.FrameworkName).ToList();
            var graphs     = new List <RestoreTargetGraph>();

            foreach (var framework in frameworks)
            {
                graphs.Add(await WalkDependencies(projectRange, framework, remoteWalker, context));
            }

            if (graphs.Any(g => g.InConflict))
            {
                _log.LogError("Failed to resolve conflicts");
                return(new RestoreResult(success: false, restoreGraphs: graphs));
            }

            // Install the runtime-agnostic packages
            var allInstalledPackages = new HashSet <LibraryIdentity>();
            var localRepository      = new NuGetv3LocalRepository(request.PackagesDirectory, checkPackageIdCase: false);

            await InstallPackages(graphs, request.PackagesDirectory, allInstalledPackages, request.MaxDegreeOfConcurrency);

            // Resolve runtime dependencies
            var runtimeGraphs = new List <RestoreTargetGraph>();

            if (request.Project.RuntimeGraph.Runtimes.Count > 0)
            {
                foreach (var graph in graphs)
                {
                    var runtimeSpecificGraphs = await WalkRuntimeDependencies(projectRange, graph, request.Project.RuntimeGraph, remoteWalker, context, localRepository);

                    runtimeGraphs.AddRange(runtimeSpecificGraphs);
                }

                graphs.AddRange(runtimeGraphs);

                if (runtimeGraphs.Any(g => g.InConflict))
                {
                    _log.LogError("Failed to resolve conflicts");
                    return(new RestoreResult(success: false, restoreGraphs: graphs));
                }

                // Install runtime-specific packages
                await InstallPackages(runtimeGraphs, request.PackagesDirectory, allInstalledPackages, request.MaxDegreeOfConcurrency);
            }
            else
            {
                _log.LogVerbose("Skipping runtime dependency walk, no runtimes defined in project.json");
            }

            // Build the lock file
            var repository     = new NuGetv3LocalRepository(request.PackagesDirectory, checkPackageIdCase: false);
            var lockFile       = CreateLockFile(request.Project, graphs, repository);
            var lockFileFormat = new LockFileFormat();

            lockFileFormat.Write(projectLockFilePath, lockFile);

            return(new RestoreResult(true, graphs, lockFile));
        }
Esempio n. 4
0
        public static Assembly TryLoadHelp(this Assembly assembly, ZipPackage zipPackage, ILogger logger)
        {
            logger.LogInformation("Looking for xml help for {0}", assembly.Name);
            var helpFile = zipPackage.GetFiles().FirstOrDefault(file => file.Path.Equals(assembly.Path.Replace("dll", "xml"), StringComparison.OrdinalIgnoreCase));

            if (helpFile != null) {
                logger.LogInformation("Found xml help for {0}", assembly.Name);
                var xml = XDocument.Load(helpFile.GetStream());
                foreach (var member in xml.Element("doc").Element("members").Elements("member")) {
                    var name = member.Attribute("name")?.Value;
                    if (name == null) {
                        continue;
                    }

                    try {
                        switch (name.First()) {
                            case 'T': {
                                assembly.FindType(name.Substring(2))?.LoadHelp(member);
                                break;
                            }

                            case 'M': {
                                var argStart = name.IndexOf('(');
                                if (argStart < 2) {
                                    continue;
                                }
                                var methodName = name.Substring(2, argStart - 2);
                                var typeName = methodName.Substring(0, methodName.LastIndexOf('.'));
                                methodName = methodName.Substring(methodName.LastIndexOf('.') + 1);
                                assembly.FindType(typeName)?.Methods.FirstOrDefault(m => m.Name == methodName)?.LoadHelp(member);
                                break;
                            }
                        }
                    } catch (Exception ex) {
                        logger.LogError("Failed to pull out help details", ex);
                    }
                }
            }

            return assembly;
        }