コード例 #1
0
 /// <summary>
 /// Returns the key to use identify a unique visitor.
 /// </summary>
 /// <param name="args">The log arguments.</param>
 /// <returns>The key.</returns>
 private string GetVisitorKey(LogAnalyzerArgs args)
 {
     return(args.VisitorCookie ?? "NULL");
 }
コード例 #2
0
        /// <summary>
        /// Submits a log tag and entry originally generated by <see cref="WebHelper.GetRequestLogTag" />
        /// and <see cref="WebHelper.GetRequestLogEntry" /> for analysis.
        /// </summary>
        /// <param name="visitorCookie">The unique visitor cookie or <c>null</c>.</param>
        /// <param name="logTag">The log tag.</param>
        /// <param name="logEntry">The log entry.</param>
        /// <remarks>
        /// <para>
        /// This method raises the <see cref="AnalyzeEvent" /> for each log entry submitted, providing
        /// a way for applications to completely customize how the event is processed.  Event handlers
        /// can set the <see cref="LogAnalyzerArgs.Handled" /> property to <c>true</c> to indicate that
        /// no further processing of the evend should be performed or leave this as <c>false</c> to
        /// allow processing to continue.
        /// </para>
        /// <para>
        /// After raising the event and if the handler indicates that processing should continue,
        /// the method will perform default processing to determine whether the logged entry refers
        /// to a page view, a unique, visitor or is a search engine related operation.
        /// </para>
        /// <para>
        /// The <paramref name="visitorCookie" /> is used to help determine whether the log entry
        /// should be counted as a unique visitor or not.  This may be passed as <c>null</c> in which
        /// case the method will attempt to extract the standard LillTek unique visitor cookie from
        /// the logged request.  <see cref="AnalyzeEvent" /> handles may override this beghavior by
        /// setting the <see cref="LogAnalyzerArgs.VisitorCookie" /> property in the event arguments
        /// before returning.
        /// </para>
        /// </remarks>
        public void Submit(string visitorCookie, string logTag, string logEntry)
        {
            try
            {
                var args = new LogAnalyzerArgs(visitorCookie, logTag, logEntry);

                if (AnalyzeEvent != null)
                {
                    AnalyzeEvent(this, args);

                    if (args.Handled)
                    {
                        return;
                    }
                }

                logTag = args.LogTag.ToLower();

                // Count page hits.

                if (logTag == "pageview")
                {
                    if (args.IsPageView.HasValue)
                    {
                        if (args.IsPageView.Value)
                        {
                            this.PageViews++;
                        }
                    }
                    else if (pageExtensions.ContainsKey(Path.GetExtension(args.Path)))
                    {
                        args.IsPageView = true;
                        this.PageViews++;
                    }
                    else
                    {
                        args.IsPageView = false;
                    }
                }
                else
                {
                    args.IsPageView = false;
                }

                // Update the unique visitor and postback counts for page views.

                if (args.IsPageView.Value)
                {
                    uniqueVisitors[GetVisitorKey(args)] = true;
                    this.Visitors = uniqueVisitors.Count;

                    if (args.Method == "POST")
                    {
                        this.Postbacks++;
                    }
                }

                // Count search crawlers.

                if (logTag.StartsWith("botview"))
                {
                    if (logTag.Contains("google"))
                    {
                        this.GoogleCrawl++;
                    }
                    else if (logTag.Contains("bing"))
                    {
                        this.BingCrawl++;
                    }
                    else if (logTag.Contains("baidu"))
                    {
                        this.BaiduCrawl++;
                    }
                    else
                    {
                        this.OtherCrawl++;
                    }
                }

                // Count search referrals.

                if (args.Referer != null)
                {
                    var referer = args.Referer.ToString().ToLower();

                    if (referer.Contains("google"))
                    {
                        this.GoogleSearch++;
                    }
                    else if (referer.Contains("bing"))
                    {
                        this.BingSearch++;
                    }
                    else if (referer.Contains("baidu"))
                    {
                        this.BaiduSearch++;
                    }
                    else
                    {
                        // I'm going to consider requests with as referer URI that includes
                        // a "q=" query parameter as a referral from another search engine.

                        try
                        {
                            var query = Helper.ParseUriQuery(args.Referer);

                            if (query.ContainsKey("q"))
                            {
                                this.OtherSearch++;
                            }
                        }
                        catch
                        {
                            // Ignorning
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SysLog.LogException(e);
            }
        }