Exemplo n.º 1
0
        public void TestRepository()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["PelorusTraceConnection"];
            var repository = new ApplicationLogRepository(connectionString.ConnectionString);
            var log = repository.GetById(18);

            Assert.IsNotNull(log);
        }
        /// <summary>
        /// Executes the stored procedure to insert a trace message into SQL.
        /// </summary>
        /// <param name="message">Trace message to insert into the database.</param>
        /// <param name="helpLink">Help link to insert into the database.</param>
        /// <param name="source">Source of the trace message to insert into the database.</param>
        /// <param name="stackTrace">Stack trace of the trace message to insert into the database.</param>
        /// <param name="traceData">Data associated with the trace event.</param>
        /// <param name="traceEventType">Type of the trace event.</param>
        /// <param name="traceId">Id of the trace message to insert into the database.</param>
        /// <param name="correlationId">Optional correlation Id of the trace message to insert into the database.</param>
        /// <param name="correlationIndex">Optional correlation index of the trace message to insert into the database.</param>
        /// <param name="processId">Process Id to insert into the database.</param>
        /// <param name="threadId">Thread Id to insert into the database.</param>
        /// <returns>Primary key Id of the record that was inserted into that database.</returns>
        private long InsertMessage(
            string message,
            string helpLink,
            string source,
            string stackTrace,
            XmlDocument traceData,
            TraceEventType traceEventType,
            int traceId,
            Nullable<Guid> correlationId,
            Nullable<short> correlationIndex,
            int processId,
            string threadId)
        {
            string connectionString = this.GetConnectionString();
            var assembly = Assembly.GetExecutingAssembly();
            var assemblyName = assembly.GetName();
            var repository = new ApplicationLogRepository(connectionString);
            var applicationLog = new ApplicationLogDao
            {
                AppDomainName = AppDomain.CurrentDomain.FriendlyName,
                Assembly = new AssemblyDao
                {
                    AssemblyFullName = assemblyName.FullName,
                    AssemblyName = assemblyName.Name,
                    VersionBuild = assemblyName.Version.Build,
                    VersionMajor = assemblyName.Version.Major,
                    VersionMinor = assemblyName.Version.Minor,
                    VersionRevision = assemblyName.Version.Revision
                },
                CorrelationId = correlationId,
                CorrelationIndex = correlationIndex,
                Data = traceData,
                HelpLink = helpLink,
                MachineName = Environment.MachineName,
                Message = message,
                ProcessId = processId,
                Source = source,
                StackTrace = stackTrace,
                ThreadId = threadId,
                TraceEventType = traceEventType,
                TraceId = traceId,
                TraceListenerName = this.Name
            };
            long applicationLogId = repository.Create(applicationLog);

            return applicationLogId;
        }
        /// <summary>
        /// Return a specific application log by Id.
        /// </summary>
        /// <param name="context">
        /// An System.Web.HttpContext object that provides references to the intrinsic server objects (for example, Request, Response, Session, and 
        /// Server) used to service HTTP requests.
        /// </param>
        /// <param name="itemId">Id of the application log to return.</param>
        private void ReturnApplicationLogItem(HttpContext context, int itemId)
        {
            var config = CoreWebConfiguration.Configuration.ApplicationLogRssFeed;
            var connectionStringName = config.ConnectionString.Name;

            if (string.IsNullOrWhiteSpace(connectionStringName))
            {
                connectionStringName = DefaultConnectionStringName;
            }

            var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
            var applicationLogRepository = new ApplicationLogRepository(connectionString.ConnectionString);
            var log = applicationLogRepository.GetById(itemId);

            var xmlDocument = new System.Xml.XmlDocument();
            var xmlNavigator = xmlDocument.CreateNavigator();

            using (var writer = xmlNavigator.AppendChild())
            {
                var serializer = new DataContractSerializer(typeof(ApplicationLogDao));
                serializer.WriteObject(writer, log);
            }

            if (null != log.Data.FirstChild)
            {
                var dataElement = xmlDocument.CreateElement("Data", xmlDocument.FirstChild.NamespaceURI);
                var importedElement = xmlDocument.ImportNode(log.Data.FirstChild, true);
                dataElement.AppendChild(importedElement);
                xmlDocument.FirstChild.AppendChild(dataElement);
            }

            context.Response.ContentType = "text/xml";
            context.Response.Output.Write(xmlDocument.InnerXml);
            context.Response.StatusCode = (int) HttpStatusCode.OK;
            context.Response.Flush();
        }
        /// <summary>
        /// Gets the items for the RSS feed from the repository.
        /// </summary>
        /// <param name="channelName">Name of the RSS channel.</param>
        /// <param name="thisUrl">URL to this RSS feed.</param>
        /// <returns>Collection of RSS items for the feed.</returns>
        private RssItem[] GetItems(string channelName, string thisUrl)
        {
            var config = CoreWebConfiguration.Configuration.ApplicationLogRssFeed;
            var connectionStringName = config.ConnectionString.Name;

            if (string.IsNullOrWhiteSpace(connectionStringName))
            {
                connectionStringName = DefaultConnectionStringName;
            }

            var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];

            if (null == connectionString)
            {
                throw new ConfigurationErrorsException($"Connection string '{connectionStringName}' was not found.");
            }

            var applicationLogRepository = new ApplicationLogRepository(connectionString.ConnectionString);
            var sixMonths = new TimeSpan(180, 0, 0, 0);
            var logs = applicationLogRepository.GetSinceDate(DateTime.UtcNow.Subtract(sixMonths));
            var items = new Collection<RssItem>();

            foreach (var log in logs)
            {
                string uniqueId = log.Id.ToString().ToBase64String();
                items.Add(new RssItem
                {
                    GloballyUniqueIdentifier = uniqueId,
                    Description = log.Message,
                    Link = $"{thisUrl}?item={uniqueId}",
                    PublishDate = log.CreatedOn,
                    Source = new RssSource
                    {
                        Url = thisUrl,
                        Value = channelName
                    },
                    Title = $"{log.TraceListenerName}: {log.TraceEventType} - {log.Message}"
                });
            }

            return items.ToArray();
        }