C#Log日志

引用log4net






logsLog.cs


using System;
using System.Text;


using System.IO;
using System.Windows.Forms;


namespace AutoSend
{
    class Log
    {
        #region 输出DEBUG 信息
        /// <summary>
        /// 输出DEBUG 信息
        /// </summary>
        /// <param name="value">信息</param>
        /// <param name="type"></param>
        #endregion
        public  void Debug(String value,int type)
        {
            WriteFarmat(value);
        }


        public  void Info(String value, int type)
        {
            WriteFarmat(value);
        }


        public  void Error(String value, int type)
        {
            WriteFarmat(value);
        }


        public  void Warn(String value, int type)
        {
            WriteFarmat(value);
        }


        public  void Final(String value, int type)
        {
            WriteFarmat(value);
        }




        #region 输出DEBUG 信息
        /// <summary>
        /// 输出DEBUG 信息
        /// </summary>
        /// <param name="value">信息</param>
        /// <param name="type"></param>
        #endregion
        public void Debug(String value)
        {
            WriteFarmat(value);
        }


        public void Info(String value)
        {
            WriteFarmat(value);
        }


        public void Error(String value)
        {
            WriteFarmat(value);
        }


        public void Warn(String value)
        {
            WriteFarmat(value);
        }


        public void Final(String value)
        {
            WriteFarmat(value);
        }




        public  bool IsDebugEnable
        {
            get { return true; }
        }


        public  bool IsInfoEnable
        {
            get { return true; }
        }


        public  bool IsWarnEnable
        {
            get { return true; }
        }




        public  bool IsErrorEnable
        {
            get { return true; }
        }




        public  bool IsFanilEnable
        {
            get { return true; }
        }


        private String WriteFarmat(String message)
        {
            //  String time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            // String value ="\n" + time + " " +message;
            // Write(value);
            log.Debug(message);
            return "";
        }
        log4net.ILog log = log4net.LogManager.GetLogger("JFStateManager");


        private static void Write(String message)
        {
            string strLuJing = Application.StartupPath + "\\" + "logs\\timely" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            StreamWriter sw = new StreamWriter(strLuJing, true, Encoding.UTF8);
            sw.Write(message);
            sw.Close();
        }
    }
    public class LogAddress
    { 
        public static String ALL ="0";


        public static String BACK ="1";


        public static String FRONT = "2";




    }


}


config下的log4net.xml


<?xml version="1.0" encoding="utf-8" ?>
<configuration>


  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>


  <log4net debug="true">
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
      <param name="File" value="logs/AutoSendLog.log" />
      <param name="datePattern" value="MM-dd HH:mm" />
      <param name="AppendToFile" value="true" />


      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="---------------START:\r\n" />
        <param name="Footer" value="---------------End\r\n"/>
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>


    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="logs/AutoSendLog.log" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="10" />
      <param name="MaximumFileSize" value="5MB" />
      <param name="RollingStyle" value="Date" />
      <param name="StaticLogFileName" value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="--START:\r\n" />
        <param name="Footer" value="--End\r\n"/>
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m %F %L %n" />
      </layout>
    </appender>
    
    <root>
      <level value="DEBUG" />
      <!-- <appender-ref ref="LogFileAppender" />  -->
        <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>




</configuration>



MainForm.cs使用Log

using System;
using System.Windows.Forms;
using AutoSend;


namespace SysChargeInterface
{
    public partial class MainForm : Form
    {


        public MainForm()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {


        }


        private void Form1_Load_1(object sender, EventArgs e)
        {


        }


        public void WirtelogTh(String msg)
        {
            this.rtbLog.Invoke(new EventHandler(delegate
            {
                if (rtbLog.TextLength + msg.Length >= rtbLog.MaxLength)
                    rtbLog.Text = "";
                rtbLog.AppendText(msg);
                rtbLog.Focus();
            }));
        }
        //getCharge提交订单
        Log log = new Log();
        private void btnGetCharge_Click(object sender, EventArgs e)
        {
            //if (tbxAgentId.Text.ToString() == "" || tbxTelephone.Text.ToString() == "" || tbxTelephoneType.Text.ToString() == "" || tbxMoney.Text.ToString() == "" || tbxChargeMode.Text.ToString() == "" || tbxChargeMode.Text.ToString() == "" || tbxRequestId.Text.ToString() == "" || tbxExtendInfo.Text.ToString() == "" || tbxCallBackUrl.Text.ToString() == "" || tbxGetChargeUrl.Text.ToString() == "" || tbxMD5Key.Text.ToString() == "")
            //{
            //    MessageBox.Show("必要参数填写不完整!");
            //}
            //else
            //{
                String SubTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:s");//订单日期
                String AgentId = tbxAgentId.Text.ToString();
                String Telephone = tbxTelephone.Text.ToString();
                String TelephoneType = tbxTelephoneType.Text.ToString();
                String Money = tbxMoney.Text.ToString();
                String ChargeMode = tbxChargeMode.Text.ToString();
                String RequestID = tbxRequestId.Text.ToString();
                String ExtendInfo = tbxExtendInfo.Text.ToString();
                String CallBackUrl = tbxCallBackUrl.Text.ToString();
                String getChargeUrl = tbxGetChargeUrl.Text.ToString();
                String MD5Key = tbxMD5Key.Text.ToString();
                String para = "AgentID=" + AgentId + "&Telephone=" + Telephone + "&TelephoneType=" + TelephoneType + "&Money=" + Money
                    + "&ChargeMode=" + ChargeMode + "&RequestID=" + RequestID + "&ExtendInfo=" + ExtendInfo + "&CallBackUrl=" + CallBackUrl + "&SubTime=" + SubTime;
                String Hmac = Share.MD5(para + "&Key=" + MD5Key);
                String url = getChargeUrl + "/boinf_getCharge.do?" + para + "&Hmac=" + Hmac;
                AutoSend.View.write("请求Url:" + url);
                log.Debug("请求Url:" + url);
                string result = Share.getPage(url, null);
                AutoSend.View.write("提交返回:"+result);
                log.Debug("提交返回:" + result);


            //}
        }
        //Query
        private void btnQuery_Click(object sender, EventArgs e)
        {
            //if (tbxAgentId.Text.ToString() == "" || tbxRequestId2.Text.ToString() == "" || tbxOrderID.Text.ToString() == "" || tbxGetBalanceUrl.Text.ToString() == "" || tbxMD5Key.Text.ToString() == "")
            //{
            //    MessageBox.Show("必要参数填写不完整!");
            //}
            //else
            //{
                String AgentId = tbxAgentId.Text.ToString();
                String RequestID = tbxRequestId2.Text.ToString();
                String OrderID = tbxOrderID.Text.ToString();
                String getBalanceUrl = tbxGetBalanceUrl.Text.ToString();
                String MD5Key = tbxMD5Key.Text.ToString();
                String para = "AgentID=" + AgentId + "&OrderID=" + OrderID + "&RequestID=" + RequestID;
                String Hmac = Share.MD5(para + "&Key=" + MD5Key);
                String url = getBalanceUrl + "/boinf_QueryChargeRecord.do?" + para + "&Hmac=" + Hmac;
                AutoSend.View.write("请求Url:" + url);
                log.Debug("请求Url:" + url);
                string result = Share.getPage(url, null);
                AutoSend.View.write("充值结果查询返回:" + result);
                log.Debug("充值结果查询返回:" + result);
            //}
        }


        private void btnGetBalance_Click(object sender, EventArgs e)
        {
            //if (tbxAgentId.Text.ToString() == "" || tbxQueryUrl.Text.ToString() == "" || tbxMD5Key.Text.ToString() == "")
            //{
            //    MessageBox.Show("必要参数填写不完整!");
            //}
            //else
            //{
                String AgentId = tbxAgentId.Text.ToString();
                String queryUrl = tbxQueryUrl.Text.ToString();
                String MD5Key = tbxMD5Key.Text.ToString();
                String para = "AgentID=" + AgentId;
                String Hmac = Share.MD5(para + "&Key=" + MD5Key);
                String url = queryUrl + "/boinf_getBalance.do?" + para + "&Hmac=" + Hmac;
                AutoSend.View.write("请求Url:" + url);
                log.Debug("请求Url:" + url);
                string result = Share.getPage(url, null);
                AutoSend.View.write("代理商查询返回:" + result);
                log.Debug("代理商查询返回:" + result);
            //}
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值