AppendAllText写垃圾

时间:2015-10-07 03:15:30

标签: c# ssis

我在SSIS脚本任务中有以下内容:

但是,当我查看输出文本文件时,我得到一个乱码字符串。

public void Main()
{

    string variableValue = Dts.Variables["Failure_Reason"].Value.ToString();
    string outputFile = Dts.Variables["ErrorLog"].Value.ToString();

    System.IO.File.AppendAllText(outputFile, variableValue);


    // TODO: Add your code here
    Dts.TaskResult = (int)ScriptResults.Success;

当我在脚本中放置断点时,我看到了正确的信息:

  

"查找工作号码时失败。\ r \ n该项目   导致失败的是:\ r \ nLine:D \ r \ nQuantity:1 \ r \ nFootage:   1.166667 \ r \ n"

我尝试将编码更改为UTF-8和ASCII,但我仍然感到垃圾。有什么理由吗?

1 个答案:

答案 0 :(得分:1)

适合我。我怀疑你是否简化了问题,你的初始日志中包含了时髦或unicode数据,或者填充Failure_Reason的进程有BOM或类似的东西。来自轨道的Nuke日志文件并尝试重新运行。

我的包装似乎是

control flow

结果

enter image description here

文字输出

enter image description here

BIML

商业智能标记语言Biml允许我使用一些XML来定义SSIS包的外观,然后您,读者可以在家中使用它来重现我的结果。这样可以省去几百个截图的麻烦,而且你可以正确配置每个角落和裂缝。

您需要做的就是为visual studio / bid / SSDT安装名为BIDS Helper的免费开源附加组件

安装完成后,您将右键单击现有的SSIS项目并选择"添加新的Biml文件"

在新创建的BimlScript.biml的内容中,粘贴以下代码(2005/2008所需的小修改)。

右键单击biml文件,然后选择Generate SSIS Package。此时,您应该拥有上述软件包并可以在C:\ ssisdata中查看它创建日志文件,除非您更改下面的第90行

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <ScriptProjects>
        <ScriptTaskProject ProjectCoreName="ST_32983202" Name="ST_32983202" VstaMajorVersion="0">
            <ReadOnlyVariables>
                <Variable Namespace="User" VariableName="Failure_Reason" DataType="String" />
                <Variable Namespace="User" VariableName="ErrorLog" DataType="String" />
            </ReadOnlyVariables>
            <Files>
                <File Path="ScriptMain.cs" BuildAction="Compile">using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_32983202
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            bool fireAgain = false;

            string variableValue = Dts.Variables["Failure_Reason"].Value.ToString();
            string outputFile = Dts.Variables["ErrorLog"].Value.ToString();
            System.IO.File.AppendAllText(outputFile, variableValue);

            string message = variableValue;
            Dts.Events.FireInformation(0, "variableValue", message, string.Empty, 0, ref fireAgain);

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
    }
}               </File>
                <File Path="Properties\AssemblyInfo.cs" BuildAction="Compile">
using System.Reflection;
using System.Runtime.CompilerServices;

//
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("AssemblyTitle")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ProductName")]
[assembly: AssemblyCopyright("Copyright @  2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.*")]
                </File>
            </Files>
            <AssemblyReferences>
                <AssemblyReference AssemblyPath="System" />
                <AssemblyReference AssemblyPath="System.Data" />
                <AssemblyReference AssemblyPath="System.Windows.Forms" />
                <AssemblyReference AssemblyPath="System.Xml" />
                <AssemblyReference AssemblyPath="Microsoft.SqlServer.ManagedDTS.dll" />
                <AssemblyReference AssemblyPath="Microsoft.SqlServer.ScriptTask.dll" />
            </AssemblyReferences>
        </ScriptTaskProject>
    </ScriptProjects>
    <Packages>
        <Package Name="so_32983202" ConstraintMode="Linear">
            <Variables>
                <Variable DataType="String" Name="Failure_Reason"><![CDATA[There was a failure looking up the Job Number.
The item that caused the failure was: 
Line: D
Quantity: 1
Footage: 1.166667
]]></Variable>
                <Variable DataType="String" Name="ErrorLog">C:\ssisdata\so_32983202.log</Variable>
            </Variables>
            <Tasks>
                <Script ProjectCoreName="ST_32983202" Name="SCR Do Stuff">
                    <ScriptTaskProjectReference ScriptTaskProjectName="ST_32983202" />
                </Script>
            </Tasks>
        </Package>
    </Packages>
</Biml>