[ASP.NET] 基于.Net Remoting 的网站访问监控模块

监控公司局域网的网站访问情况。
项目1:
新建类MonitorHttpModule.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
namespace HouseProgram.HttpModule
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class MonitorHttpModule :IHttpModule
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public void Init(HttpApplication application)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (ChannelServices.RegisteredChannels.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ChannelServices.RegisterChannel(
new TcpClientChannel(), false);
            }
           

            application.BeginRequest 
+= new EventHandler(application_BeginRequest);
        }


        
void application_BeginRequest(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                HttpContext context 
= ((HttpApplication)sender).Context;
                MonitorClass obj 
= (MonitorClass)Activator.GetObject(typeof(MonitorClass), "tcp://sz389:8085/HouseProgramMonitor");
                
if (obj == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    StringBuilder sb 
= new StringBuilder();
                    sb.AppendLine(String.Format(
"Time:[ {0} ]  IP:[ {1} ]  Type:[ {2} ]", DateTime.Now.ToString(), context.Request.UserHostAddress, context.Request.RequestType));
                    
if (!String.IsNullOrEmpty(context.Request.ContentType))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sb.AppendLine(context.Request.ContentType);
                    }

                    
if (!String.IsNullOrEmpty(context.Request.Form.ToString()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        sb.AppendLine(context.Request.Form.ToString().Replace(
"&","\r\n"));
                    }

                    sb.AppendLine(context.Request.Url.ToString());
                    obj.RecordInfo(sb.ToString(), context.Request.RequestType);
                }

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }

        }


        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

    }

}
生成dll,在站点的Web.config 文件中加入以下节点:
ContractedBlock.gif ExpandedBlockStart.gif Code
<system.web>
    
<httpModules>
        
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        
<add name="MonitorHttpModule" type="HouseProgram.HttpModule.MonitorHttpModule"/>
    
</httpModules>
</system.web>

项目2:
新建类MonitorClass.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
public class MonitorClass : MarshalByRefObject
    {
        
public MonitorClass()
        {
            
//Console.WriteLine(String.Format("Client Constructor Called  DateTime: [ {0} ]", DateTime.Now.ToString()));
        }

        
~MonitorClass()
        {
            
//Console.WriteLine(String.Format("Client Destructor Called  DateTime: [ {0} ]", DateTime.Now.ToString()));
        }

        
public delegate void RecordInfoHandler(string msg, string requestType);
        
public static event RecordInfoHandler OnRecordInfoEvent;

        
public void RecordInfo(string msg, string requestType)
        {
            
if (OnRecordInfoEvent != null)
            {
                OnRecordInfoEvent(msg, requestType);
            }
        }
    }

项目3:
新建Console 项目
ContractedBlock.gif ExpandedBlockStart.gif Code
namespace HouseProgram.RemotingServer
{
    
class Program
    {
        
static void Main(string[] args)
        {
            TcpServerChannel channel 
= new TcpServerChannel(8085);

            ChannelServices.RegisterChannel(channel, 
false);

            RemotingConfiguration.RegisterWellKnownServiceType(
typeof(MonitorClass), "HouseProgramMonitor", WellKnownObjectMode.SingleCall);
            Console.BufferHeight 
= 30000;
            MonitorClass.OnRecordInfoEvent 
+= new MonitorClass.RecordInfoHandler(MonitorClass_OnRecordInfoEvent);            
            Console.WriteLine(
"Press Enter to exit.");
            Console.ReadLine(); 
        }
        
static void MonitorClass_OnRecordInfoEvent(string msg, string requestType)
        {
            
switch (requestType)
            {
                
case "POST":
                    Console.ForegroundColor 
= ConsoleColor.Yellow;
                    Console.WriteLine(msg);
                    
break;
                
case "GET":
                    Console.ForegroundColor 
= ConsoleColor.DarkGreen;
                    Console.WriteLine(msg);
                    
break;
                
default:
                    Console.ForegroundColor 
= ConsoleColor.Cyan;
                    Console.WriteLine(msg);
                    
break;
            }
            
        }
    }
}

转载于:https://www.cnblogs.com/wyforumid/archive/2009/09/22/1571939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值