winform:使用log4net向SqlServer2008R2中插入自定义字段的问题

配置文件App.config
<?xml version="1.0"?>








<!--
-->




  <commandText value="INSERT INTO logData ([ID],[logTime],[userID],[moduleId],[opeaterContent],[remark]) VALUES (@ID, @logTime, @userID, @moduleId,@opeaterContent, @remark)"/>

  <parameter>
    <parameterName value="@ID" />
    <dbType value="String" />
    <size value="50" />
    <layout type="Log4ToSqlServer.MyLogDataBaseLayout">
      <conversionPattern value="%Id" />
    </layout>
  </parameter>

  <parameter>
    <parameterName value="@logTime" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>

  <parameter>
    <parameterName value="@userID" />
    <dbType value="String" />
    <size value="50" />
    <layout type="Log4ToSqlServer.MyLogDataBaseLayout">
      <conversionPattern value="%UserId" />
    </layout>
  </parameter>

  <paramter>
    <parameterName value="@moduleId" />
    <dbType value="String" />
    <size value="50" />
    <layout type="Log4ToSqlServer.MyLogDataBaseLayout">
      <conversionPattern value="%ModuleId" />
    </layout>
  </paramter>

  <parameter>
    <parameterName value="@opeaterContent" />
    <dbType value="String" />
    <size value="500" />
    <layout type="Log4ToSqlServer.MyLogDataBaseLayout">
      <conversionPattern value="%OpeaterContent" />
    </layout>
  </parameter>

  <parameter>
    <parameterName value="@remark" />
    <dbType value="String" />
    <size value="200" />
    <layout type="Log4ToSqlServer.MyLogDataBaseLayout">
      <conversionPattern value="%Remark" />
    </layout>
  </parameter>
</appender>




自定义Layout和Convert如下所示:
namespace Log4ToSqlServer
{
public class MyLogDataBaseLayout : PatternLayout
{
public MyLogDataBaseLayout()
{
this.AddConverter("Id", typeof(IdPatternConverter));
this.AddConverter("UserId", typeof(UserIdPatternConverter));
this.AddConverter("ModuleId", typeof(ModuleIdPatternConverter));
this.AddConverter("OpeaterContent", typeof(OpeaterContentPatternConverter));
this.AddConverter("Remark", typeof(RemarkPatternConverter));
}
}

public class IdPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var messageLog = loggingEvent.MessageObject as LogMessage;
        if (messageLog != null)
        {
            writer.Write(messageLog.Id);
        }
    }
}

public class UserIdPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var messageLog = loggingEvent.MessageObject as LogMessage;
        if (messageLog != null)
        {
            writer.Write(messageLog.UserId);
        }
    }
}

public class ModuleIdPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var messageLog = loggingEvent.MessageObject as LogMessage;
        if (messageLog != null)
        {
            writer.Write(messageLog.ModuleId);
        }
    }
}

public class OpeaterContentPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var messageLog = loggingEvent.MessageObject as LogMessage;
        if (messageLog != null)
        {
            writer.Write(messageLog.OpeaterContent);
        }
    }
}

public class RemarkPatternConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var messageLog = loggingEvent.MessageObject as LogMessage;
        if (messageLog != null)
        {
            writer.Write(messageLog.Remark);
        }
    }
}

}

对Log4Net进行了一下简单封装:
public class Log4NetHelper
{
public static readonly log4net.ILog logInfo = log4net.LogManager.GetLogger("logInfo");

    public static void WriteDatebaseLog(LogMessage logData)
    {
        if (logInfo.IsInfoEnabled)
        {
            logInfo.Info(logData);
        }
    }

    public static void WriteDatebaseLogTest(LogContent logData)
    {
        if (logInfo.IsInfoEnabled)
        {
            logInfo.Info(logData);
        }
    }
}

    调用方法:
    LogMessage logdata = new LogMessage();
        logdata.Id = Guid.NewGuid().ToString();            
        logdata.UserId = "03d17e47-d657-4eae-b7e2-b40d13e4530d";
        logdata.ModuleId = "0c83c755-09d9-4c3a-ae2e-a42155c4cf1f";
        logdata.OpeaterContent = DateTime.Now.ToString() + "OperationContent";
        logdata.Remark = DateTime.Now.ToString() + "remark";

        //LogContent log = new LogContent();
        //log.Event_Type = 1;

        Log4NetHelper.WriteDatebaseLog(logdata);

在AssemblyInfo.cs中添加:[assembly: log4net.Config.XmlConfigurator(Watch=true)]

项目使用的是.net 4.0,log4net.dll使用的1.2.11.0。

数据表创建SQL:
CREATE TABLE [dbo].logData NOT NULL,
[logTime] [datetime] NULL,
[userID] varchar NULL,
[moduleId] varchar NULL,
[opeaterContent] nvarchar NULL,
[remark] nvarchar NULL,
CONSTRAINT [PK_LOGDATA] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

现在的问题是不提示结果也插不进数据,跪求大神指点。。。

net

1个回答

问题解决,配置文件错了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐