Log4Net,如何获取存储过程的结果?

时间:2016-07-22 12:16:21

标签: c# stored-procedures log4net log4net-appender

我正在开发C#,我正在寻找一种有效记录日志文件和数据库的方法。我找到了Log4Net。我的appender工作正常,我可以在我的数据库中写。我想知道是否可以通过Log4Net获取存储过程的结果。

我解释说:我有两个appender(" Add"" Update")调用两个不同的存储过程(" sp_AjouterEntree"" sp_UpdateEntree& #34;)在SQLserver上。第一个过程向表中添加一行,第二个过程用一条错误消息(如果有的话)和" EndDate"更新该行。任务。

表的主键是ID。该ID由第一个程序自动选择(" Add")。 我需要的是收到第一个程序的返回@@ identite,以便知道我需要在第二个appender中更新哪一行"更新"。

我希望我的问题足够明确。 我在其他讨论中找到了答案,但显然之前没有人遇到过这个问题。

感谢您的帮助。

存储过程(添加行):

ALTER PROCEDURE [dbo].[sp_AjouteEntree]
@Flux varchar(50), @Etape varchar(200),@Debut datetime2(7),@Fin datetime2(7),@EtapeKO bit, @Erreur varchar(max) , @Annexe varchar(max)
AS
BEGIN



INSERT INTO [dbo].[Journal]
       ([Jour_Flux]
       ,[Jour_Etape_Flux]
       ,[Jour_Date_Heure_Debut]
       ,[Jour_Date_Heure_Fin]
       ,[Jour_Etape_KO]
       ,[Jour_Erreur]
       ,[Jour_Annexe])
 VALUES
       (@Flux
       ,@Etape
       ,@Debut
       ,@Fin
       ,@EtapeKO
       ,@Erreur
       ,@Annexe)



return @@IDENTITY
END

存储过程(更新行):

 ALTER PROCEDURE [dbo].[sp_UpdateEntree]
 @Id integer,@Fin datetime2(7),@EtapeKO bit, @Erreur varchar(max) ,@Annexe varchar(max)
 AS
 BEGIN


UPDATE [dbo].[Journal]
SET [Jour_Date_Heure_Fin] = @Fin
  ,[Jour_Etape_KO] = @EtapeKO
  ,[Jour_Erreur] = @Erreur
  ,[Jour_Annexe] = @Annexe
WHERE [Jour_ID]=@Id


END

第一个appender:

<appender name="Add" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<threshold value="WARN" /> 
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
<connectionString value="data source=******************;Database=Test_Log4Net;Trusted_Connection=yes" />

<commandText value="sp_AjouteEntree" />
<commandType value="StoredProcedure"/>

<parameter>
  <parameterName value="@Flux" />
  <dbType value="AnsiString" />
  <size value="50" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{FluxName}" />
  </layout>
</parameter>

<parameter>
  <parameterName value="@Etape" />
  <dbType value="AnsiString" />
  <size value="500" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%method" />
  </layout>
</parameter>

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

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

<parameter>
  <parameterName value="@EtapeKO" />
  <dbType value="Byte" />
  <size value="1" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{ErreurKO}" />
  </layout>
</parameter>

<parameter>
  <parameterName value="@Erreur" />
  <dbType value="AnsiString" />
  <size value="2000" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="NULL" />
  </layout>
</parameter>

<parameter>
  <parameterName value="@Annexe" />
  <dbType value="AnsiString" />
  <size value="5000" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message" />
  </layout>
</parameter>

第二个appender:

<appender name="Update" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />

<threshold value="WARN" />

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

<connectionString value="data source=*****************;Database=Test_Log4Net;Trusted_Connection=yes" />

<commandText value="sp_UpdateEntree" />
<commandType value="StoredProcedure"/>

<parameter>
  <parameterName value="@Id" />
  <dbType value="Int32" />
  <size value="50" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{ID}" />
  </layout>
</parameter>

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

<parameter>
  <parameterName value="@EtapeKO" />
  <dbType value="Byte" />
  <size value="1" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{ErreurKO}" />
  </layout>
</parameter>

<parameter>
  <parameterName value="@Erreur" />
  <dbType value="AnsiString" />
  <size value="2000" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%property{messageErreur}" />
  </layout>
</parameter>

<parameter>
  <parameterName value="@Annexe" />
  <dbType value="AnsiString" />
  <size value="5000" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message" />
  </layout>
</parameter>

1 个答案:

答案 0 :(得分:-1)

这是我的存储过程代码

public MapMarker makeTargetSmallBubbleMarker(Route route, Address address) {
    // obtain view
    View v = inflater.inflate(R.layout.info_bubble_small, null);

    // get the value..
    String destination = "";

    // inflate value into view sub fields
    ((TextView) v.findViewById(R.id.destination)).setText(destination);

    // if you use 9-patch bubble with donwside arrow for background of your marker
    // you need to change anchor point of the marker, so that the arrow will point to location
    // instead of bubble appears above the destination

    // maybe someone knows a better solution..? Calculate rendered view size
    v.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    int width = v.getMeasuredWidth();
    int height = v.getMeasuredHeight();
    // make view screen-shot
    Image bubbleImg = new Image();
    bubbleImg.setBitmap(loadBitmapFromView(v, width, height));

    MapMarker bubble = new MapMarker(route.getDestination(), bubbleImg);
    // here, I want my bubble arrow and whole marker get a 20% offset for down arrow
    bubble.setAnchorPoint(new PointF(width / 2f, height * 1.2f));

    return bubble;
}


private static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    //v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.layout(0, 0, width, height);
    v.draw(c);
    return b;
}